Possible Duplicate:
initializer_list and move semantics
Environment: Linux, g++-4.7
I used std::vector and a class of my own to test this. And I found that when using the std::initializer_list to construct a vector, it actually calls the copy constructor of the custom class to make a temporary object. Therefore, I consider it really unefficient and I used "const std::initializer_list & li" to replace it.
Why is it really common in STL library?? Eg:
// This is in STL: stl_vector.h
vector(initializer_list<value_type> __l, const allocator_type & __a = allocator_type())
//...
Is there actually something skiped my mind?
My test code is shown below:
#include <iostream>
#include <vector>
#include <initializer_list>
class Test
{
public:
Test(const Test & t) : v(t.v) {
std::cout << "Copy Cons" << std::endl;
}
Test(Test && t) : v(std::move(t.v)) {
std::cout << "MC" << std::endl;
}
Test(int val) : v(val) {}
private:
int v;
};
int main()
{
std::vector<Test> vv({Test(0), Test(1), Test(2)});
return 0;
}//main
It's output:
Copy Cons
Copy Cons
Copy Cons