tags:

views:

86

answers:

2

I've got a type that can't be moved or copied (by making the necessary constructors and operators private). But when I tried to compile a std::list of them, the operation failed with a very strange error (class name snipped for brevity).

1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xmemory(202)
: error C2248: 'T::T' : cannot access private member declared in class 'T'

Surely it's not incumbent of a type in a linked list to be movable or copyable.

When these members are made public, the code compiles fine- even though, if the std::list had tried to access them, it would be an unresolved external, since they're only declared private. Makes no sense :(

+7  A: 

As of C++03, elements must be copy constructible and copy assignable. §23.1/3:

The type of objects stored in these components must meet the requirements of CopyConstructible types (20.1.3), and the additional requirements of Assignable types.

In C++0x, requirements are put on a per-operation basis, but in general it's safe to say elements must be move constructible and move assignable. (Though some operations require copy constructibility and assign-ability, etc.)

A typical solution to your problem is to store pointers to objects, via shared_ptr or some other smart pointer.

GMan
But the code doesn't generate an error if they're public. Which it should, if they're required to be copyable, since they're only declared.
DeadMG
@DeadMG, that's because the compiler is eliding the copy and therefore not requiring the function to be defined. It still has to be accessible though.
avakar
That explains it. A pity that I can't mark a comment as an answer.
DeadMG
A: 

standard containers need their element type to be copyable, if you have noncopyable types, use (smart) pointers as the element type instead (like: std::list<boost::shared_ptr<YourType> >.

template class/functions often generate errors unless you use them explicitly, so it could be that you havent got an error.

smerlin