Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9033517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:15:08+00:00 2026-06-16T08:15:08+00:00

To demonstrate my problem, consider this simple program that does not compile: #include <boost/noncopyable.hpp>

  • 0

To demonstrate my problem, consider this simple program that does not compile:

#include <boost/noncopyable.hpp>
#include <unordered_map>

class foo : boost::noncopyable { };

int main()
{
    std::unordered_map<int, foo> m;
    auto & element = m[0];

    return 0;
}

Using the current version of boost (1.52), Visual Studio 2012 returns the error:
cannot access private member declared in class 'boost::noncopyable_::noncopyable.

The operator [] for std::unordered_map returns a reference to the element at the provided key, which at first glance seems like it should work — I’ve asked for a reference to the element, not a copy of it.

My understanding of the problem is this (which might be wrong, as I haven’t used C++ in a while). If the key is not found, unordered_map creates a new element and returns a reference to the new element. boost::noncopyable defines a (private) copy constructor but not a move constructor, and so a move constructor is not generated by the compiler. In its operator[], std::unordered_map makes use of std::move, but since boost::noncopyable doesn’t define a move constructor, it falls back to the copy constructor. Since the copy constructor is private, the compilation fails.

What prompted this post is that I’m trying to create an unordered_map of boost::signal2::signal, which inherits from boost::noncopyable. Short of hacking the boost library, is there a simple workaround I can do? Wrapping the signal in a unique_ptr is an option, but it seems to me I might be doing something wrong here.

Update:

I may have posted too soon! It appears impossible to add a subclass of boost::noncopyable to unordered_map. Insert, operator[], and emplace all use either a copy constructor (which is private), or a move operation (which doesn’t exist for boost::noncopyable). To me this seems a major limitation. Is it even possible to create an unordered_map that contains boost::noncopyable objects? I’m explicitly not trying to copy them — I want them to spend their entire lifespan inside the unordered_map.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T08:15:10+00:00Added an answer on June 16, 2026 at 8:15 am

    It’s not impossible to use a subclass of boost::noncopyable in an unordered_map, you simply have to define a move constructor for you type. C++ does not create a default move constructor if you’ve made your own copy construct (which is what boost::noncopyable does). Also, if it did define default move constructor, it would try to call the parent’s copy constructor which is private. So you must define a move constructor that doesn’t try to call boost::noncopyable‘s copy constructor. For example this works fine:

    #include <boost/noncopyable.hpp>
    #include <unordered_map>
    
    struct foo : public boost::noncopyable
    {
        foo() = default;
        foo(foo&&) {}
    };
    
    int main()
    {
        std::unordered_map<int, foo> m;
        auto & element = m[0];
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created this simple android Activity to demonstrate my problem. I just want
I made this simple application to demonstrate my problem. It has: An image A
Here is a JSFiddle that demonstrates the problem... http://jsfiddle.net/L2NBP/5/ For obvious reasons this code
I've been writing a test case program to demonstrate a problem with a larger
I just encountered this (made up code to demonstrate the problem): public ICollection<string> CreateCollection(int
I've simplified this just to show what is needed to demonstrate the problem -
Consider the following table with a constraint that's a bit daft but simple enough
i'll use some sample code to demonstrate my problem... this is an entity public
I made this fiddle to demonstrate the problem. Table margins don't collapse in Firefox,
I have this code to demonstrate the problem: public static void main(String[] args) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.