Re: constructor for POD types?
* George:
When implementing a general template class, sometimes we call T() -- suppose
T is type argument of a template class.
My questions,
1. what will happen if T is POD type? Do nothing?
By C++98, default-initialization. By C++03, value-initialization. Essentially
C++03 value-initialization does a better (more thorough) job for non-PODs.
2. Is it good code? Or working but not good code?
Don't know, you haven't shown any of the cases where "sometimes we call T()".
Here is my test code, works in MSVC 2008.
[Code]
template <class T> class Foo {
public:
void static test()
{
T(); // call constructor for any type, including POD?
This (1) allocates a temporary, (2) calls the T default constructor, (3) calls
the T destructor, (4) deallocates the temporary.
}
};
int main()
{
Foo<int> g;
g.test();
return 0;
This "return" isn't necessary in C++.
}
[/Code]
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?