模板函数:在C++中没有复制构造的默认构造

xto*_*ofl 6 c++ templates copy-constructor default-constructor

考虑到

struct C { 
   C()            { printf("C::C()\n"          ); }
   C(int)         { printf("C::C(int)\n"       ); }
   C( const C& )  { printf("copy-constructed\n"); }
};
Run Code Online (Sandbox Code Playgroud)

和模板功能

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 e.g. 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  >();
}
Run Code Online (Sandbox Code Playgroud)

看一下t1,T例如,它不会被初始化int.另一方面,t2将从默认构造的临时复制构造.

问题是:在C++中是否可以默认构造一个通用变量,而不是使用template-fu?

Jam*_*lis 5

这是一个你可以使用的技巧,使用本地类:

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

    THolder t1; // t1.obj is value-initialized
}
Run Code Online (Sandbox Code Playgroud)

我想我从另一个Stack Overflow问题的答案中读到了这个技巧,但我现在无法找到这个问题.

或者,您可以使用boost::value_initialized<T>类模板,它基本上做同样的事情,具有更大的灵活性和一致性,并为错误的编译器提供变通方法.

在C++ 0x中,它更容易:您可以使用空的初始化列表:

T obj{}; // obj is value-initialized
Run Code Online (Sandbox Code Playgroud)

(据我所知,只有gcc 4.5+支持C++ 0x初始化列表.Clang和Visual C++还不支持它们.)