如何检查新的 codecvt_byname 构建是否成功
是否有标准方法来检查新的 std::codecvt_byname
的构造是否成功?
我正在尝试以下程序:
// cl /nologo /Fetest_codecvt_byname.exe /EHsc test_codecvt_byname.cpp && test_codecvt_byname
// g++ -o test_codecvt_byname test_codecvt_byname.cpp && test_codecvt_byname
#include <cstdlib>
#include <iostream>
#include <locale>
#include <new>
#include <stdexcept>
int main()
{
try {
new std::codecvt_byname<wchar_t, char, mbstate_t>(".nonsense");
} catch (const std::exception& ex) {
std::cerr << "Error: " << ex.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
如果指定的语言环境不受支持,Windows 上的 libstdc++ 显然会抛出 std::runtime_error
对象。然而,Microsoft Visual C++ 的 STL 实现不会引发异常。
不知道哪个 C++ 编译器将编译代码,如何检查新的 std::codecvt_byname
构造是否成功?或者,有没有办法在没有内存不足的情况下检查构建是否会成功?
Is there a standard way to check whether construction of a new std::codecvt_byname
succeeded?
I was experimenting with the following program:
// cl /nologo /Fetest_codecvt_byname.exe /EHsc test_codecvt_byname.cpp && test_codecvt_byname
// g++ -o test_codecvt_byname test_codecvt_byname.cpp && test_codecvt_byname
#include <cstdlib>
#include <iostream>
#include <locale>
#include <new>
#include <stdexcept>
int main()
{
try {
new std::codecvt_byname<wchar_t, char, mbstate_t>(".nonsense");
} catch (const std::exception& ex) {
std::cerr << "Error: " << ex.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
libstdc++ on Windows apparently throws a std::runtime_error
object if the named locale is unsupported. Microsoft Visual C++'s STL implementation, however, does not throw an exception.
Not knowing which C++ compiler will compile the code, how do I check whether construction of the new std::codecvt_byname
succeeded? Alternatively, is there a way to check whether construction will be successful assuming no out-of-memory scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C++11 FDIS 的 [22.3.1.1.2] 节,
locale::facet
类指出:不幸的是,如果指定的语言环境无效,标准不要求 std::codecvt_byname 构造函数抛出异常,显式
std::locale
构造函数也是如此。 >区域设置(const char*)。但是,解决方法是尝试构建区域设置和use_facet< /code>
codecvt
方面,而不是尝试使用std::codecvt_byname
。Section [22.3.1.1.2], Class
locale::facet
, of the C++11 FDIS states:The Standard unfortunately does not require an exception to be thrown by the
std::codecvt_byname
constructor if the named locale is invalid, as does the explicitstd::locale
constructorlocale(const char*)
. However, a work-around is to attempt to construct the locale anduse_facet
thecodecvt
facet instead of attempting to usestd::codecvt_byname
.