我可以重载 std::string 构造函数吗?
我想创建一个构造函数,它接受 std::wstring 并返回一个 std::string。这可能吗?如何实现?
谢谢。
最佳答案
Can I overload the std::string constructor?
不,它需要更改 std::string
声明。
I want to create a constructor which takes std::wstring and return a std::string. is it possible and how?
您可以改用转换函数,例如:
std::string to_string(std::wstring const& src);
但是,您需要决定如何处理无法使用 std::string
的 8 位编码表示的符号:是将它们转换为多字节符号还是抛出一个异常(exception)。参见 wcsrtombs功能。
关于c++ - 重载 std::string 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9262091/