M
mzdude
I have the following code
struct foo
{
operator std::string () const { return str;}
std::string f() const {return str;}
std::string str;
};
struct bar : private foo
{
using foo:perator std::string;
using foo::f;
};
void test()
{
bar b;
std::string a = b; // Ok
std::string a2 = b.f(); // Ok
const bar b2;
std::string a3 = b2; // Compile error
std::string a4 = b2.f(); // Ok
}
My question is why does the compiler have a problem with the const
for the operator but not the function.
error XXXX: 'initializing' : cannot convert from 'const bar' to
'std::basic_string<_Elem,_Traits,_Ax>'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
No constructor could take the source type, or constructor
overload resolution was ambiguous
struct foo
{
operator std::string () const { return str;}
std::string f() const {return str;}
std::string str;
};
struct bar : private foo
{
using foo:perator std::string;
using foo::f;
};
void test()
{
bar b;
std::string a = b; // Ok
std::string a2 = b.f(); // Ok
const bar b2;
std::string a3 = b2; // Compile error
std::string a4 = b2.f(); // Ok
}
My question is why does the compiler have a problem with the const
for the operator but not the function.
error XXXX: 'initializing' : cannot convert from 'const bar' to
'std::basic_string<_Elem,_Traits,_Ax>'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
No constructor could take the source type, or constructor
overload resolution was ambiguous