Template functions: building by default without copying in C ++

Considering

struct C { C() { printf("C::C()\n" ); } C(int) { printf("C::C(int)\n" ); } C( const C& ) { printf("copy-constructed\n"); } }; 

And the template function

 template< typename T > void foo(){ // default-construct a temporary variable of type T // this is what the question is about. T t1; // will be uninitialized for eg int, float, ... T t2 = T(); // will call default constructor, then copy constructor... :( T t3(); // deception: this is a local function declaration :( } int main(){ foo<int>(); foo<C >(); } 

Looking at t1 , it will not be initialized when T is, for example. int On the other hand, t2 will be copied from the default temporary.

Question: is it possible in C ++ to build a shared variable different from template-fu by default?

+6
c ++ copy-constructor templates default-constructor
source share
4 answers

Here is a trick you can use using a local class:

 template <typename T> void foo() { struct THolder { T obj; THolder() : obj() { } // value-initialize obj }; THolder t1; // t1.obj is value-initialized } 

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.)

+5
source share

If you don't care that the copy constructor must exist, and just want it not to be called:

Do not worry: this will not happen. In this situation, the call to copy constructor in will be canceled. Always and reliably - even when compiling with optimizations disabled ( -O0 ).

+2
source share

What is your real question? The default constructor is invoked for instance t1.

0
source share

T t2 = T (); // will call the default constructor, then copy the constructor ...: (

Not for my compiler (VC2008). The way out for me is ...

 C::C() C::C() 

This is what I expect from this. Did I miss something?

0
source share

All Articles