字符串会自动将常量字符* 转换为 std::string
string automatically converts const char* to std::string
我在旧编译器中使用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
?
这是预期的行为吗?
是的,这是意料之中的,因为相关的构造函数std::string::string(const char*)
不是explicit
,这意味着它可用于这样的隐式转换。
相关文章:
- 将无符号char*转换为std::istream*C++
- 可以有效地转换 std::any 与 std::any_cast
- 尝试转换 std::chrono::d uration 会导致"rep cannot be a duration"编译错误
- 封送处理 - 转换 std::向量<char>到字符串^ 反之亦然
- 在 C++ 中转换 std::string,使其可以是 C# 中的 byte[]
- 转换 std::vector<T> 为 char *
- 转换std ::绑定到功能指针
- 强制转换 std::vector 的所有成员
- 无法转换'std::string{又名std::basic_string<char>}'到&
- 转换std::string到cv::Mat对象
- 转换std::chrono::system_clock::time_point::min()为字符串时,无效空指针错误
- 简单的模板化函数,用于转换 std::vectors - "illegal use of this type as an expression"
- Const转换std容器
- 如何强制转换std::make_unique,以便可以使用在类中声明的函数
- 如何向下转换std::shared_ptr
- 转换 std::vector<uint8_t> 为 QImage
- 使用std::dynamic_pointer_cast向上转换std::shared_ptr
- 如何转换std::vector<无符号字符>到向量<char>而无需复制
- 正在强制转换std::pair
const&T1 const, T2>const&安全的 - 如何在boost.python中转换std::string*