(v1);" to fail A vector(v1) expression gives a temporary object and can be placed on the right side of operator...">

Why the operator "vector <int> (v1);" to fail

A vector<int>(v1) expression gives a temporary object and can be placed on the right side of operator= , but if we use the vector<int>(v1) expression as an instruction, it will not be executed in Visual Studio 2010 10.0.30319.1 RTMRel , For detailed error information, see the comments in the following code. Why is this happening?

 vector<int> v1; v1.push_back( 10 ); v1.push_back( 20 ); v1.push_back( 30 ); vector<int> v3 = vector<int>(v1); //OK, deliberately code like this. vector<int>(v1); //error C2086: "std::vector<_Ty> v1": redefinition 

In C ++ Coding Standards: 101 Rules, Recommendations, and Best Practices, chapter 82, “Use accepted idioms to really reduce capacity and really erase items.” There is a statement:

 container<_Type>(c).swap(c); 

I do not understand and just want to check container<_Type>(c) , what does this mean?

+6
source share
3 answers

I understand that vector<int>(v1) contrary to intuition, but I see no reason to use it. As others have noted, this is standard behavior. If you are looking for a solution, here are 2 methods:

 vector<int>::vector( v1 ); vector<int> { v1 }; 

EDIT (question changed): " container<_Type>(c).swap(c); I do not understand ..." This is different from container<_Type>(c); . A container may have a capacity greater than that specified through size (see reserve ). Reserve helps minimize the number of certain operations. If you add a new item, the container uses the already allocated memory. For example, your vector may have space for 10 elements, but in reality it has only 1. If you add a new element, there will be space for 8 elements. The above design removes the reserve to save memory. First, a copy of the original is made (this copy has no reserve). Then the basic data (pointer) of the original vector is replaced by the new (see swap ) new, and the temporary object (which now owns the original memory) is discarded.

"... check container<_Type>(c) , what does this mean?" Used as above, means "create a temporary copy of c". Used in isolation, it looks like a copy constructor, but in fact it declares an object. The difference is set by the point operator.

+1
source

vector<int>(v1); matches vector<int> v1; . those. redefinition of a variable.

+6
source

vector<int>(v1) expression gives a temporary object and can be placed on the right side of operator= , but if we use vector<int>(v1) as an instruction, we fail ...

A simple statement is handled differently by the compiler:

 vector<int>(v1); //error C2086: "std::vector<_Ty> v1": redefinition 

Is an alternative way to record

 vector<int> v1; 

So, you override v1 and the compiler complains.


To view your temporary initialization, use, for example,

 void foo(const std::vector<int>& v) { } 

and call

 foo(vector<int>(v1)); 

or just 1

 (std::vector<int>)(v1); // this creates a temporary which is immediately disposed 

See live demo for the latter


1) Stolen from @Sergey A answer , but he preferred to remove it

+4
source

All Articles