Here is a trick you can use using a local class:
template <typename T> void foo() { struct THolder { T obj; THolder() : obj() { }
I think I read about this trick from the answer to another question about stack overflow, but at the moment I can not find this question.
Alternatively, you can use the boost::value_initialized<T> class template, which basically does the same, with more flexibility and consistency, and workarounds for error compilers.
In C ++ 0x, this is much simpler: you can use an empty initializer list:
T obj{}; // obj is value-initialized
(As far as I know, only gcc 4.5+ supports C ++ 0x initializer lists. Clang and Visual C ++ do not yet support them.)
James McNellis
source share