What are you compiling that with? It doesn't work.
157.cpp:6:8: error: cannot define the implicit default assignment operator for 'struct node<int>', because non-static reference member 'data' can't use default assignment operator
"test.cpp", line 6: error #2397: implicitly generated assignment operator cannot copy:
reference member "node<T>::data [with T=int]"
IBM
"test.cpp", line 9.14: 1540-0718 (S) An implicit copy assignment operator cannot be created for class with a member of type "const int &".
"test.cpp", line 6.8: 1540-0719 (I) The previous message was produced while processing the implicit member function "node<int>::operator=(const node<int> &)".
Sun
Error: node<int> has a reference member data and cannot be assigned.
Where: While instantiating "std::vector<node<int> >::__insert_aux(node<int>*, const node<int>&)".
I think I have just found a very usefull bug *happy*
These errors are all about the implicit compiler generated copy assignment a.k.a assignment operator
(=) - This is an issue because the assignment operator uses
existing objects ( existing object = existing object) and so will
bail if the class contains const or & variables.
The compiler can create a copy constructor as this is just an
memory overlay (and the object on the left hand side is a new as yet
unitialised object.
The copy constructor is all that isreally need in the above code
(although some compiler implementation details may cause the code to fail)
False. When you have references as members of a class, they are commonly implemented as pointers (though it depends on the implementation):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
class a
{
char *p;
};
class b
{
char &r;
b(); //these are just so the example compiles
b(const b &);
b &operator=(const b &);
};
int main()
{
std::cout << sizeof(a) << std::endl;
std::cout << sizeof(b) << std::endl;
}
@LB
Ok so that is contrary to output my program generated when compiled using msvc++. If we assume that references are implemented as pointers, there would only be 4 objects in that vector, but there are 50
Now I am even more confused...
I should really stop writing these proof of concept programs...
A structure can't magically change its size during runtime, it's always a compile-time constant.
vectors dynamically allocate memory for their elements and keep a pointer to it. nodes.capacity()*sizeof(node) tells you how much memory it actually allocated.
"For vector and deque, T shall also be CopyAssignable"
This occurs in the context of specific expressions/operations involving sequence containers. None of those are used here. If you include an operation that does have this requirement, the compilation fails. (Even without an operation with that requirement invoked, VC++ generates a warning that operator= could not be generated.)
Marking as solved.
Thanks everyone, especially LB and Athar.
Did some more testing and it looks like those indeed are implemented as pointers, and sizeof(node) is 4.
If you include an operation that does have this requirement, the compilation fails
On further reading, you're right vector::push_back only requires CopyInsertable. It's vector::insert that requires CopyAssignable that I quoted earlier.