I have a class C that has a cast operator. In the example, I tried to cast an instance of it to std::string three different ways: static_cast , constructor std::string and assigning std::string . However, compiling only the latest compilation, while others create an ambiguous constructor error.
The cause of the error is clear enough: there are many ways to convert C to what the std::string constructor can take. But what is the difference between these cases? Why does operator casting work as intended, but not there?
struct C { template<typename T> operator T() const { return T{}; } }; int main() { C c; cout << static_cast<string>(c) << endl;
UPD : as mentioned in the comments of the balls, this question does not reproduce with C ++ 17. I tested it with g ++ - 5 and clang-3.8 with -std = C ++ 11 and -std = C ++ 14, and it shows described errors.
c ++ casting initialization language-lawyer templates
Ivan Smirnov
source share