I have the 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 it happens to fail. The message returned by 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 for call to ‘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 the second constructor came? I really have no clue…
Other answers discuss why you’re getting a compile-time failure (which is what most questions are about when such failures are the most prominent part of the question). However. regarding your explicit question, “whence the second constructor came?”:
12.8/4 “Copying class objects” of standard says:
If you’d like to avoid the implicit copy-ctor from being used, one of a few ‘non-copyable’ idioms can be used (such as
boost::noncopyable): http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-copyable_Mixin