Reputation: 957
Suppose I have a function template and want to declare a value-initialized object:
template<typename T>
void foo() {
// declare and default-initialize 'x' of type 'T'
}
Can I do it?
T x;
fails for primitive types because it leaves them uninitialized,T x();
fails because of the most vexing parseT x = T();
requires a copy constructor and doesn't require the compiler elide the copyT x{};
fails because we're not using C++11.I'm hoping I'm being an idiot and overlooking something obvious, but I'm not seeing the answer.
Upvotes: 2
Views: 133
Reputation: 548
Pre c++11
T x = T();
read here - link
T3 var3 = {};
The third form, T3 var3 = {} initializes an aggregate, typically a "C-style" struct or a "C-style" array. However, the syntax is not allowed for a class that has an explicitly declared constructor.
Upvotes: 1