std::locale::global 的问题

来源:互联网 发布:c 并发编程实践 pdf 编辑:程序博客网 时间:2024/06/09 23:49
相信大家都碰到过VS2005和VS2008中使用std::ofstream打开包含中文的文件目录的问题吧。

解决方法是网上一致传诵的:
std::locale::global(std::locale("")); // 解决std::ofstream.open()中文路径文件名问题

global函数应该返回前一个locale对象,但是看了它的源码,返回的是 _Oldglobal,而这个变量自从定义以后就没有被“加工”过,怎么可以实现功能呢?


以下代码来自 Microsoft Visual Studio 9.0\VC\crt\src\locale.cpp
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#if !STDCPP_IMPLIB || !defined(_M_CEE_PURE)
 
_MRTIMP2 locale __cdecl locale::global(const locale& loc)
    {   // change global locale
    locale _Oldglobal;
    _BEGIN_LOCK(_LOCK_LOCALE)
        locale::_Locimp *_Ptr = _Getgloballocale();
 
        if (_Ptr != loc._Ptr)
            {   // set new global locale
            _DELETE_CRT(_Ptr->_Decref());
            _Setgloballocale(_Ptr = loc._Ptr);
            _Ptr->_Incref();
            category _Cmask = _Ptr->_Catmask & all;
            if (_Cmask == all)
                setlocale(LC_ALL, _Ptr->_Name.c_str());
            else
                for (int catindex = 0; catindex <= _X_MAX; ++catindex)
                    if ((_CATMASK(catindex) & _Cmask) != 0)
                        setlocale(_CAT_TO_LC(catindex), _Ptr->_Name.c_str());
            }
        return (_Oldglobal);
    _END_LOCK()
    }
 
#endif // STDCPP_IMPLIB || !defined(_M_CEE_PURE)
原创粉丝点击