Given the following code, why doesn't the compiler resolve the implicit conversion when constructing Bar
? That is, construct Foo
just like a
was constructed which is (should) then be used to construct Bar
?
#include <string>
class ImplicitlyConvertToChar
{
public:
ImplicitlyConvertToChar(const char* a_char)
: m_string(a_char)
{ }
ImplicitlyConvertToChar(const char* a_char, size_t a_end)
: m_string(a_char)
{
}
template <typename T_String>
ImplicitlyConvertToChar(T_String const& a_string)
: m_string(a_string.begin())
{
}
operator char const * () const
{ return m_string; }
const char* m_string;
};
class Foo
{
public:
Foo(const ImplicitlyConvertToChar& a_charLike)
: m_string(a_charLike)
{ }
const char* m_string;
};
class Bar
{
public:
Bar(const Foo& a_foo)
: m_foo(a_foo)
{ }
Foo m_foo;
};
int main()
{
Foo a("this works");
Bar b("Why doesn't this?");
}
You are not allowed more than one user defined implicit conversion. The Foo
example involves one, the Bar
example involves two.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With