首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“charset”不更改`wchar_t`的字符集

“charset”不更改`wchar_t`的字符集
EN

Stack Overflow用户
提问于 2020-08-25 10:30:47
回答 1查看 282关注 0票数 0

下面是简单的C++代码。

代码语言:javascript
复制
#include <iostream>
#include <locale>

int main() {
    std::wcout.sync_with_stdio(false);
    std::wcout.imbue(std::locale(""));
    std::cout << std::locale("").name() << std::endl; // C.UTF-8
    
    wchar_t s {L'\xd0b8'};
    std::wcout << s << std::endl;
    // expected: и
    // actual: 킸
}
  • '\xd0b8'在UTF-8中是и,在UTF-16中是

参考文献提到:

窄的多字节字符串文本(1)和宽字符串文本(2)的编码是实现定义的。例如,gcc使用命令行选项fexec-charset和-fwide-exec-charset来选择它们。

我在Ubuntu20.04.1LTS (WSL)中使用g++ -O3 -fwide-exec-charset=UTF-8 source.cc -o out.a && out.a编写并执行了代码,预期输出是и,但是实际输出是。g++不处理文字字符串中的十六进制,该字符串用于直接初始化宽字符s。如何使g++考虑UTF-8中的宽焦?

系统信息:

代码语言:javascript
复制
> lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:        20.04
Codename:       focal
> g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.3.0-10ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-25 12:14:59

man gcc

代码语言:javascript
复制
  -fwide-exec-charset=charset
       Set the wide execution character set, used for wide string and character
       constants.  The default is UTF-32 or UTF-16, whichever corresponds to the width
       of "wchar_t".  As with -fexec-charset, charset can be any encoding supported by
       the system's "iconv" library routine; however, you will have problems with
       encodings that do not fit exactly in "wchar_t".

UTF-8是一种不适合于wchar_t的多字节编码.因此,您得到了文档中指定的内容:问题。因为Linux上的sizeof(wchar_t)是4,所以需要指定一个4字节范围的编码,比如UTF-32。

除了与遗留代码的接口之外,没有真正的理由使用wchar_t

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63576955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档