検討中
struct C {
C() { printf("C::C()\n" ); }
C(int) { printf("C::C(int)\n" ); }
C( const C& ) { printf("copy-constructed\n"); }
};
そしてテンプレート機能
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 >();
}
を見ると、例えばのt1
場合は初期化されません。一方、一時的に構築されたデフォルトからコピー構築されます。T
int
t2
質問: C++ では、template-fu を使用する以外に、ジェネリック変数をデフォルトで構築することは可能ですか?