为什么显式运算符 std::string 不起作用
why explicit operator std::string does not work
我有一个类,它有一个类型std::string的转换运算符标记为显式。这是类
class MyClass {
public:
// methods ...
explicit operator std::string() const {
// convert to string ...
}
}
问题是当我在 MyClass 类型的变量上使用 static_cast 时,我收到错误"static_cast从'MyClass'到'std::string aka ..."当我为任何自定义类型定义转换运算符时,我似乎遇到了同样的问题。显式修饰符是否仅用于转换为基元类型,或者这是另一个编译器错误。
下面是一个例子
#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;
}
输出为
Hello World
通过更新到最新版本的LLVM解决了问题,该版本完全支持所有C++11功能。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- std::string == 运算符在代码中不起作用
- 为什么unordered_set<string::iterator>不起作用?
- 如何通过长度从 std::vector 中删除元素<string>(擦除不起作用)
- 将char转换为std::string不起作用
- 为什么 std::list<string> 有效,但 std::list <int>不起作用
- 为什么显式运算符 std::string 不起作用
- 为什么一个简单的bimap(std::string <-> int)不起作用?
- std::string 的函数指针的类型定义不起作用
- 字符串运算符 += 有时在 string::resize() 之后不起作用
- 为什么 getline() 在 C++ 中不起作用?(调用'getline(std::ofstream&, std::string&)'没有匹配函数
- 在 C++11 中初始化 std::vector<std::string>不起作用?
- QString到std::string到const char*的转换在赋值中不起作用
- std::地图.使用c-string进行计数不起作用
- 为什么我的读取矢量<矢量>的循环<string>不起作用?Linux C++
- 从 std::string 到类的隐式转换不起作用
- Compare string对cin不起作用
- <string> 矢量在使用单独的类时不起作用
- 为什么在 std::string 上重载"operator <<"不起作用?
- 使用 std::string.compare() 将不起作用