jeudi 25 décembre 2014

Why is this user-defined conversion failing?

This code fails but I think the conversion function chosen should be operator std::string() since it takes less steps (Foo -> std::string) than the operator const char*() (Foo -> const char* -> std::string).



struct Foo
{
operator const char*();
operator std::string();
};

void f(Foo foo)
{
std::string s2(foo); // Error!
}


The standard says the conversions are limited to one conversion sequence, and that conversions are applied only where they are unambiguous, so why are these failing?


The error is:



error: call to constructor of 'std::string' (aka 'basic_string<char>') is ambiguous
std::string s2(foo);
^ ~~~
note: candidate constructor
basic_string(basic_string&& __str)
^
note: candidate constructor
basic_string(const basic_string& __str);
^
note: candidate constructor
basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
^
1 error generated.

Aucun commentaire:

Enregistrer un commentaire