我在使用std::out_of_range constructor
代码与较旧的编译器时收到“没有构造函数的实例”错误时偶然发现了这个错误
对于一些较旧的编译器,std::out_of_range
的定义是
class out_of_range : public logic_error {
public:
out_of_range(const string& message);
};
在更新的版本中,他们添加了第二个构造函数
class out_of_range : public logic_error {
public:
out_of_range(const string& message);
out_of_range(const char *message);
};
我尝试编译的代码执行了以下构造函数调用:
std::out_of_range("Some Error Message");
添加#include <string>
后
编译器能够将const char*
转换为std::string
?
这是预期的行为吗?
答案 0 :(得分:5)
是的,这是预期的,因为相关构造函数std::string::string(const char*)
不 explicit
,这意味着它可用于隐式< / em>像这样的转换。