As I know std::allocator<T>::construct
takes only two parameters on older version of C++; the first is a pointer to raw, un-constructed memory in which we want to construct an object of type T
and the second one is a value of element type to initialize that object. So the copy-constructor is invoked:
struct Foo {
Foo(int, int) { cout << "Foo(int, int)" << endl; }
/*explicit*/ Foo(int) { cout << "Foo(int)" << endl; }
Foo(const Foo&) { cout << "Foo(const Foo&)" << endl; }
};
int main(int argc, char* argv[]) {
allocator<Foo> a;
Foo* const p = a.allocate(200, NULL); // second parameter is required on C++98 but on C++11 it is optional
// Foo* const p = a.allocate(200); // works fine on C++11 but not on C++98
a.construct(p, 5, 7); // works on C++ 11 and up but not C++98
a.construct(p, 10);// works on both
a.destroy(p);
a.destroy(p + 1);
a.deallocate(p, 200);
std::cout << std::endl;
}
Why on C++98
a.construct(p, 10)
calling the copy constructor but on C++11 and above is calling just the constructor that takes an integer?Does this mean on C++ 11 because of some Copy-elision optimization even if the constructor
Foo(int)
isexplicit
works on such call:a.construct(p, 5)
works on C++11 even the constructor isexplicit
what I am sure of is it doesn't works on C++98 ifFoo(int)
isexplicit
.If so then if I compile that statement with some sort of disabling
copy-elision
optimization will cause the compiler fail? Thank you.