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

标签 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的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/

相关文章:

c++ - 引用 "auto"函数作为模板参数

c++ - VS2008 SP1 : No appropriate default constructor available when pushing a pair into vector

python - 将稀疏矩阵转换为密集矩阵并获得完整的特征值

c++ - STL unordered_set如何设置初始桶计数

c - 尝试编译 TPC-H Benchmark 并返回此错误 ld : library not found for -lgcc

C 不为函数创建堆栈帧

具有 1 个函数的 C++/CLI 库与 native C++ 静态链接

c++ - 从 Linux 到 Windows 交叉编译 wxWidgets 代码

c++ - 将零参数包传递给 printf

c++ - VS2012 - Decltype 作为尾随返回类型中的模板参数