EvanED
EvanED

Reputation: 957

How to value-initialize an object of a template type in C++03

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?

I'm hoping I'm being an idiot and overlooking something obvious, but I'm not seeing the answer.

Upvotes: 2

Views: 133

Answers (1)

Tarik Neaj
Tarik Neaj

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.

source

Upvotes: 1

Related Questions