gpt4 book ai didi

c++ - 这是gcc的错误吗?

转载 作者:可可西里 更新时间:2023-11-01 16:37:20 26 4
gpt4 key购买 nike

#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的bug吗?

最佳答案

Is this a bug of 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 (确定为非缺陷)。

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

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

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

关于c++ - 这是gcc的错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41744559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com