I donβt understand why the code below prints struct Value instead of int (which implies that the conversion constructor is converted to Value instead of int ). (Visual C ++ 2012)
Why is this happening? Why does the compiler completely ignore the Value(int) constructor?
#include <iostream> #include <type_info> using namespace std; struct Value { Value(int) { } }; struct Convertible { template<class T> operator T() const { throw typeid(T).name(); } }; int main() { try { Value w((Convertible())); } catch (char const *s) { cerr << s << endl; } }
Edit:
This is even stranger (this time it is only C ++ 11, on GCC 4.7.2):
#include <iostream> #include <typeinfo> using namespace std; struct Value { Value(Value const &) = delete; Value(int) { } }; struct Convertible { template<class T> operator T() const { throw typeid(T).name(); } }; int main() { try { Value w((Convertible())); } catch (char const *s) { cerr << s << endl; } }
What gives:
source.cpp: In function 'int main()': source.cpp:21:32: error: call of overloaded 'Value(Convertible)' is ambiguous source.cpp:21:32: note: candidates are: source.cpp:9:3: note: Value::Value(int) source.cpp:8:3: note: Value::Value(const Value&) <deleted>
If the copy constructor is deleted, then why is there any ambiguity ?!
c ++ visual-c ++ implicit-conversion
Mehrdad Dec 19 '12 at 10:43 2012-12-19 10:43
source share