这是gcc的错误吗? [英] Is this a bug of gcc?

查看:181
本文介绍了这是gcc的错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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输出:

clang and vc++ are both ok, but gcc 6.2 outputs:

[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的错误吗?

推荐答案

这是gcc的错误吗?

Is this a bug of gcc?

不. std::codecvt的析构函数受保护.参见[locale.codecvt](标准草案):

No. The destructor of std::codecvt is protected. See [locale.codecvt] (standard draft):

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

显然,其他实现方式是为了提高公众的知名度而选择的,但这不是标准所必需的.

Apparently the other implementations had chosen to promote the visibility to public, but that is not required by the standard.

另请参见 LWG问题721 (确定为无缺陷").

Also see LWG issue 721 (decided as Not A Defect).

这是刻面原始设计的令人遗憾的结果.

This is a regrettable consequence of the original design of the facet.

缺陷报告还提供了有关如何构造此类对象的示例:

The defect report also has an example of how to construct such object:

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> >;

这篇关于这是gcc的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆