I have a class which has a conversion operator for type std::string marked as explicit. Here is the class
class MyClass {
public:
// methods ...
explicit operator std::string() const {
// convert to string ...
}
}
Problem is when I use static_cast on a variable of type MyClass I get the error "No matching conversion for static_cast from 'MyClass' to 'std::string aka …" I seem to have the same problem when I define conversion operators for any custom type. Is the explicit modifier only defined for conversion to primitive types or is this another compiler bug.
Here is an example
#include <iostream>
#include <string>
class MyClass {
public:
// methods ...
explicit operator std::string() const {
return "Hello World";
}
};
int main()
{
MyClass obj;
std::cout << static_cast<std::string>( obj ) << std::endl;
return 0;
}
The output is
Hello World
通过更新到最新版本的 LLVM 解决了问题,该版本完全支持所有 C++11 功能。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.