g ++ wchar_t字符串文字不是预期的类型[重复]

问题描述 投票:0回答:1

(对不起。可能不是最相关的问题...)

根据https://en.cppreference.com/w/cpp/language/string_literal:“”“宽字符串文字。L” ...“字符串文字的类型为const wchar_t [N]”“”

但是,在这种情况下,g ++似乎选择了const wchar_t *:

auto sw = L"foo";
cout << "type : " << typeid(sw).name() << " >" << sw << "<\n";
cout << "   type : " << typeid( const wchar_t * ).name() << " | type : " << typeid( const wchar_t [] ).name() << "\n";

在GCC 5.4.0上提供以下输出:

type : PKw >0x401470<
   type : PKw | type : A_w

我做对了吗?

c++ string-literals widechar
1个回答
0
投票

这是您使用的auto

auto by value will decay arrays to pointers.

[auto和宽字符串文字在这里不严格相关。

您的字符串文字确实具有类型typeid,并且(与注释部分的声明相反),它与const wchar_t[4]相同。根据链接的答案,我们可以通过切换到引用类型来抑制这种情况(坦率地说,是[[ew

):]]

const wchar_t*([auto& sw = L"foo";

© www.soinside.com 2019 - 2024. All rights reserved.