如何检查新的 codecvt_byname 构建是否成功

发布于 2024-12-18 11:44:58 字数 931 浏览 3 评论 0原文

是否有标准方法来检查新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窗影残 2024-12-25 11:44:58

C++11 FDIS 的 [22.3.1.1.2] 节,locale::facet 类指出:

对于某些标准方面,从其派生的标准“..._byname”类实现了与由 locale(const char* 构造的区域设置的方面等效的虚拟函数语义) ) 具有相同的名称。

不幸的是,如果指定的语言环境无效,标准不要求 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:

For some standard facets a standard "..._byname" class, derived from it, implements the virtual function semantics equivalent to that facet of the locale constructed by locale(const char*) with the same name.

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 explicit std::locale constructor locale(const char*). However, a work-around is to attempt to construct the locale and use_facet the codecvt facet instead of attempting to use std::codecvt_byname.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文