c++ - template of template without default constructor -
i'm trying write template class doesn't have default constructor.
a<int>
works fine, a<a<int>>
don't know how work.
1 #include <iostream> 2 using namespace std; 3 4 template <typename t> 5 class { 6 t x; 7 8 public: 9 a(t y) { x = y; } 10 }; 11 12 int main() { 13 a<int> a(0); 14 a<a<int> > b(a<int>(0)); 15 16 return 0; 17 }
error list clang
test.cpp:9:5: error: constructor 'a<a<int> >' must explicitly initialize member 'x' not have default constructor a(t y) { x = y; } ^ test.cpp:14:16: note: in instantiation of member function 'a<a<int> >::a' requested here a<a<int> > b(a<int>(0)); ^ test.cpp:6:7: note: member declared here t x; ^ test.cpp:5:9: note: 'a<int>' declared here class { ^
you not constructing x
in initializer list of constructor, hence a(t y)
must default construct x
before invoking operator=
copy assign y
it.
int
provides default constructor, let value uninitialized, a<int>
not.
your constructor should be
a(t y) : x(y) { }
Comments
Post a Comment