c++ - 当前缀字符串与非前缀字符串相邻时,字符串文字连接失败?

标签 c++ visual-studio c++11

在我认为符合 C++11 的 MSVS2013 中,编译器不喜欢以下内容:

LPCTSTR str = _T("boo " "hoo");

转化为:

wchar_t const * str = L"boo " "hoo";

根据 cppreference.com (我知道这不是确定的,但这是我目前唯一的引用):

  • 并排放置的字符串文字在编译期间连接在一起。即“你好”、“世界!”产生(单个)字符串“Hello, world!”。
    • 如果两个字符串具有相同的编码前缀(或都没有),则生成的字符串将具有相同的编码前缀(或没有前缀)。
    • 如果其中一个字符串有编码前缀而另一个没有,则认为没有的字符串与另一个字符串具有相同的编码前缀。
    • 如果 UTF-8 字符串文字和宽字符串文字并排出现,则程序格式错误。
    • 实现可能支持也可能不支持任何其他编码前缀组合。这种串联的结果是实现定义的。

重点是我自己。

谁能确认这是否符合 cppreference 指示的标准?

编辑

不喜欢,我的意思是我收到以下错误:

error C2308: concatenating mismatched strings

最佳答案

2003 ISO C++ 标准第 2.13.4p3 节说:

In translation phase 6 (2.1), adjacent narrow string literals are concatenated and adjacent wide string literals are concatenated. If a narrow string literal token is adjacent to a wide string literal token, the behavior is undefined. Characters in concatenated strings are kept distinct.

2011 年标准第 2.14.5p13 节说:

In translation phase 6 (2.2), adjacent string literals are concatenated. If both string literals have the same encoding-prefix, the resulting concatenated string literal has that encoding-prefix. If one string literal has no encoding-prefix, it is treated as a string literal of the same encoding-prefix as the other operand. If a UTF-8 string literal token is adjacent to a wide string literal token, the program is ill-formed. Any other concatenations are conditionally supported with implementation-defined behavior.

因此序列 L"boo ""hoo" 在 C2003 中具有未定义的行为,但在 C2011 中定义明确且等效于 L"boohoo"

根据您提供给我们的信息,我无法判断 MSVS2013 是否符合 C++11。你说它“不喜欢”这个构造,但如果不喜欢被表达为非致命警告并且语义符合 2011 标准中的规定,那么它可能是符合标准的。

您能否更新问题以显示诊断消息?

关于c++ - 当前缀字符串与非前缀字符串相邻时,字符串文字连接失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20687283/

相关文章:

C++ 如何格式化包含可变整数的文本行,使其在给定宽度内右对齐?

c++ - std::ostringstream 如何转换为 bool?

c++ - 为字符串生成不同的条目(要使用的点)

sql - Azure/U-SQL - 将查询输出到屏幕

c++ - 如何在不包含 Windows.h 的情况下获得 DebugBreak 的声明?

c++ - 使用在模板化类型名中定义的 typedef

.net - 如何卸载 "Microsoft .NET Core 1.0.0 RC2 - VS 2015 Tooling Preview 1"?

c++ - 枚举的 std::unordered_set,调用 find 给出段错误

C++ 在键上将两个映射相交,保留第一个映射的值

c++ - copy-and-swap 习语在 C++11 中仍然有用吗