这是gcc的错误吗?

时间:2017-01-19 14:42:11

标签: c++ gcc compiler-bug

#include <codecvt>
#include <string>
#include <locale>

std::string to_gbk(const std::wstring& u16_str)
{
        using Facet = std::codecvt_byname<wchar_t, char, std::mbstate_t>;
        std::wstring_convert
                <std::codecvt<wchar_t, char, std::mbstate_t>>
                wstr_2_gbk(new Facet("zh_CN.GBK"));

        return wstr_2_gbk.to_bytes(u16_str);
}

int main()
{
        to_gbk(L"");
}

clang和vc ++都可以,但是gcc 6.2输出:

[root@localhost ~]# g++ main.cpp 
In file included from /usr/include/c++/6.2.1/bits/locale_conv.h:41:0,
                 from /usr/include/c++/6.2.1/locale:43,
                 from main.cpp:3: /usr/include/c++/6.2.1/bits/unique_ptr.h: In instantiation of ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = std::codecvt<wchar_t, char, __mbstate_t>]’:
/usr/include/c++/6.2.1/bits/unique_ptr.h:236:17:   required from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = std::codecvt<wchar_t, char, __mbstate_t>; _Dp = std::default_delete<std::codecvt<wchar_t, char, __mbstate_t> >]’
/usr/include/c++/6.2.1/bits/locale_conv.h:218:7:   required from here
/usr/include/c++/6.2.1/bits/unique_ptr.h:76:2: error: ‘virtual std::codecvt<wchar_t, char, __mbstate_t>::~codecvt()’ is protected within this context
delete __ptr;
^~~~~~
In file included from /usr/include/c++/6.2.1/codecvt:41:0,
                 from main.cpp:1:
/usr/include/c++/6.2.1/bits/codecvt.h:426:7: note: declared protected here
       ~codecvt();
       ^

这是gcc的错误吗?

1 个答案:

答案 0 :(得分:9)

  

这是gcc的错误吗?

没有。 std::codecvt的析构函数受到保护。见[locale.codecvt](标准草案):

template <class internT, class externT, class stateT>
class codecvt : public locale::facet, public codecvt_base {
// ...
protected:
    ~codecvt();
};

显然,其他实施已选择提升公众的可见度,但标准并不要求这样做。

另见LWG issue 721(决定不是缺陷)。

  

这是该方面原始设计的一个令人遗憾的结果。

缺陷报告还有一个如何构造此类对象的示例:

template<class I, class E, class S>
struct codecvt : std::codecvt<I, E, S>
{
    ~codecvt()
    { }
}

...

std::wstring_convert<codecvt<wchar_t, char, std::mbstate_t> >;