Consider the following code:
#include <string>
class WrapperString
{
public:
WrapperString(const std::string& str) : str_(str) {}
operator std::string() const { return str_; }
operator const std::string() const { return str_; }
std::string get() const { return str_; }
private:
std::string str_;
};
int main (int, char *[])
{
WrapperString w( "Hello" );
std::string foo = w;
return 0;
}
Why is it WrapperStringreally compiled, given that two implicit conversions differ only in their constness? This result cannot be achieved by declaring a named method.
By the way, this is VS2010.
EDIT . To be clear, I added methods get()as a logical counterexample because of why it makes no sense to have two implicit conversions.
Markb source
share