As far as I read static_cast the following code should work:
#include <iostream>
#include <string>
class ConvSample
{
public:
template<typename T>
constexpr operator T(){
return {};
}
};
int main()
{
ConvSample aInst;
int i = aInst;
std::cout << i << "\n";
std::string str = static_cast<std::string>(aInst);
std::cout << str << "\n";
return 0;
}
And it works perfectly with some compilers like Clang. But e.g. with MSVC or ICC not.
In general they complain about some ambiguity caused by not really working constructors provided by std::string.
In addition if I turn on Wconversion on gcc I get a segmentation fault?!
Is there something wrong in the code? Are these errors just compiler bugs? If I change the code to not use templates it workes very well: Compiler Explorer