获取当前在C++中设置的全局区域设置

Get the global locale that is currently set in C++?

本文关键字:设置 全局 区域 C++ 获取      更新时间:2023-10-16

在C++中,我可以设置当前区域设置,如下所示:

std::locale::global(std::locale(name))

但是,我如何获得当前的全局区域设置?

在我的代码中,我需要获取当前区域设置,将其保存到tmp var中,将全局区域设置为其他区域设置,输出一些内容,然后将其设置回以前的区域设置。

如果调用std::locale的默认构造函数,就会得到它。

std::locale the_global_locale; // <-- automatically correct to std::locale::global
                               //     or a copy of std::locale::classic

更多信息请点击此处:http://en.cppreference.com/w/cpp/locale/locale/locale

它的返回值是旧的区域设置,所以只使用它。

locale l = locale::global(locale(name));
//do some stuff here
locale::global(l);

编辑:可能有用:http://en.cppreference.com/w/cpp/locale/locale/global

正如ipc所说,std::locale的默认构造函数为您提供了当前全局区域设置的副本,但为什么您需要缓存然后重置全局区域设置?

使用区域设置的C++例程通常可以使用您指定的C++区域设置对象,因此您根本不必处理全局区域设置。使用区域设置对象应该优先于使用C++全局区域设置。