【发布时间】:2014-07-16 18:39:21
【问题描述】:
根据 cplusplus.com,这是 std::runtime_error 类的实现:
class runtime_error : public exception {
public:
explicit runtime_error (const string& what_arg);
};
由于构造函数是显式的,我希望它只接受 std::string 对象。
throw std::runtime_error("error message");
不过,此代码可编译 (GCC)。编译器不应该抱怨隐式 const char* 到 const 字符串的转换吗?
【问题讨论】:
-
explicit不是这样工作的。它可以防止std::string隐式转换为std::runtime_error,而不是const char *转换为std::string。那个取决于std::string的构造函数。
标签: c++ constructor explicit