initialization of built in types in templates

G

Gernot Frisch

// THIS CODE:
template<class T> class C
{
public:
C()
{
T t;
data[0] = t;
}


T data[5];
};


int main(int, char**)
{
C<double> c;
return 0;
}

// END CODE

yields error: local variable 't' used without being initialized.
What can I do to make it work with built in variables, initialized as
0?

Must I really specialize for every built in type?
 
I

Ian Collins

Gernot said:
// THIS CODE:
template<class T> class C
{
public:
C()
{
T t;
data[0] = t;
}


T data[5];
};


int main(int, char**)
{
C<double> c;
return 0;
}

// END CODE

yields error: local variable 't' used without being initialized.
What can I do to make it work with built in variables, initialized as
0?
What do you want it to do? Is data[0] = T(); what you want?
 
K

Kai-Uwe Bux

Gernot said:
// THIS CODE:
template<class T> class C
{
public:
C()
{
T t;
data[0] = t;

What about ditching the variable t altogether:

data[0] = T();
}


T data[5];
};


int main(int, char**)
{
C<double> c;
return 0;
}

// END CODE

yields error: local variable 't' used without being initialized.
What can I do to make it work with built in variables, initialized as
0?

Must I really specialize for every built in type?

No.


Best

Kai-Uwe Bux
 
J

Jim Langston

Gernot Frisch said:
// THIS CODE:
template<class T> class C
{
public:
C()
{
T t;

T t();
data[0] = t;

Although you could just do it here:
data[0] = t();
}


T data[5];
};


int main(int, char**)
{
C<double> c;
return 0;
}

// END CODE

yields error: local variable 't' used without being initialized.
What can I do to make it work with built in variables, initialized as 0?

Must I really specialize for every built in type?
 
J

Jim Langston

Jim Langston said:
Gernot Frisch said:
// THIS CODE:
template<class T> class C
{
public:
C()
{
T t;

T t();
data[0] = t;

Although you could just do it here:
data[0] = t();

er,
data[0] = T();
is what I meant.
}


T data[5];
};


int main(int, char**)
{
C<double> c;
return 0;
}

// END CODE

yields error: local variable 't' used without being initialized.
What can I do to make it work with built in variables, initialized as 0?

Must I really specialize for every built in type?
 
D

dasjotre

didn't work for <unsigned char>. It's trying to assign a function
then.

not just for unsigned char.

T t();

declares function T (*)(void).

you either do
T t = T();
data[0] = t;

or
data[0] = T();
Ian's solution did it, though.
Thx for quick response.

check boost::value_intialized if you have to do
the same for non POD types.

regards

DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top