c++ - Trouble understanding whence a copy constructor came -
i have following small code:
template <typename t> class v { public: t x; explicit v(t & _x) :x(_x){} }; int main() { v<float> b(1.0f); // fails return 0; }
and happens fail. message returned g++ 4.4.5 is:
g++ -o0 -g3 -wall -c -fmessage-length=0 -mmd -mp -mf"main.d" -mt"main.d" -o"main.o" "../main.cpp"
../main.cpp: in function ‘int main()’:
../main.cpp:19: error: no matching function call ‘v<float>::v(float)’
../main.cpp:10: note: candidates are: v<t>::v(t&) [with t = float]
../main.cpp:6: note: v<float>::v(const v<float>&)
the thing is... whence second constructor came? have no clue...
other answers discuss why you're getting compile-time failure (which questions when such failures prominent part of question). however. regarding explicit question, "whence second constructor came?":
12.8/4 "copying class objects" of standard says:
if class definition not explicitly declare copy constructor, 1 declared implicitly.
if you'd avoid implicit copy-ctor being used, 1 of few 'non-copyable' idioms can used (such boost::noncopyable
): http://en.wikibooks.org/wiki/more_c%2b%2b_idioms/non-copyable_mixin
Comments
Post a Comment