gpt4 book ai didi

c++ - 在 VC2015 中连接不匹配的字符串 WORKS - 如何?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:26 25 4
gpt4 key购买 nike

当我们有这些之一时:

auto city1 = "New "  L"Delhi";
auto city2 = L"New " "York";

任何 VS2015 之前的编译器都会引发错误:

error C2308: concatenating mismatched strings

但是使用 VC2015 编译器,它编译得很好并且结果类型(auto 推导)是一个宽字符字符串。

我的问题是:何时以及如何做到这一点 - 任何标准规范?

最佳答案

在 C++03 中,此行为是未定义的。

ISO 14882-2003:2.13.4.3 指出

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.

不确定引入更改的确切时间,但至少在标准草案 N3242 中明确定义了行为。

ISO 14882-2011:2.14.5.13 指出

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.

因此,在您的情况下,auto 被正确推断为宽字符串文字。

关于c++ - 在 VC2015 中连接不匹配的字符串 WORKS - 如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31809141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com