By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

initialization of built in types in templates

P: n/a

// THIS CODE:
template<class Tclass C
{
public:
C()
{
T t;
data[0] = t;
}
T data[5];
};
int main(int, char**)
{
C<doublec;
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?
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

Jun 21 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Gernot Frisch wrote:
// THIS CODE:
template<class Tclass C
{
public:
C()
{
T t;
data[0] = t;
}
T data[5];
};
int main(int, char**)
{
C<doublec;
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?

--
Ian Collins.
Jun 21 '07 #2

P: n/a

"Gernot Frisch" <Me@Privacy.netwrote in message
news:5d*************@mid.individual.net...
>
// THIS CODE:
template<class Tclass 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<doublec;
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?

Jun 21 '07 #3

P: n/a
Gernot Frisch wrote:
>
// THIS CODE:
template<class Tclass 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<doublec;
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
Jun 21 '07 #4

P: n/a

"Jim Langston" <ta*******@rocketmail.comwrote in message
news:4d*************@newsfe06.lga...
>
"Gernot Frisch" <Me@Privacy.netwrote in message
news:5d*************@mid.individual.net...
>>
// THIS CODE:
template<class Tclass 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<doublec;
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?


Jun 21 '07 #5

P: n/a
>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?

Doh! thank you
Jun 21 '07 #6

P: n/a
T t();

didn't work for <unsigned char>. It's trying to assign a function
then.
Ian's solution did it, though.
Thx for quick response.
Jun 21 '07 #7

P: n/a
On 21 Jun, 09:07, "Gernot Frisch" <M...@Privacy.netwrote:
T t();

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
Jun 21 '07 #8

P: n/a
Jim Langston <ta*******@rocketmail.comwrote:
"Gernot Frisch" <Me@Privacy.netwrote in message
news:5d*************@mid.individual.net...
>>
// THIS CODE:
template<class Tclass C
{
public:
C()
{
T t;

T t();
This will declare a function called 't' that takes no parameters and
returns a T.
> data[0] = t;

Although you could just do it here:
data[0] = t();
data[0] = T(); as others have mentioned (including yourself in a later
post).

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 21 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.