#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 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();
^
Is this a bug of gcc?
Bugs should be reported to the bug database at http://gcc.gnu.org/bugs/.
Is this a bug of gcc?
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.
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> >;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With