在VC2015中连接不匹配的字符串WORKS - 如何? [英] Concatenating mismatched string WORKS in VC2015 - How?

查看:477
本文介绍了在VC2015中连接不匹配的字符串WORKS - 如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们有以下任一项时:

When we have either of these:

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

任何VS2015前编译器都会产生错误:

Any pre-VS2015 compiler would raise error:


错误C2308:连接不匹配的字符串

error C2308: concatenating mismatched strings


$ b 但是使用VC2015编译器, type( auto deduction)是一个宽字符串。

But with VC2015 compiler, it compiles well and the resultant type (auto deduction) is a wide-char string.

我的问题是:何时以及如何实现 - 任何标准规范?

My question is: When and How this is made possible - any standard specification?

推荐答案

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

In C++03 this behaviour would be undefined.

ISO 14882-2003:2.13.4.3规定

ISO 14882-2003: 2.13.4.3 states that


在翻译阶段6(2.1)中,相邻的窄字符串字符串是
连接,相邻的宽字符串字符串被连接。如果
窄字符串文字标记与宽字符串文字
标记相邻,则行为是未定义的。连接字符串中的字符
保持不同。

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.

不确定何时引入更改,

Not sure exactly when the change was introduced but the behaviour is at least well defined in the draft N3242 of the standard.

ISO 14882-2011:2.14.5.13规定

ISO 14882-2011: 2.14.5.13 states that


在翻译阶段6(2.2)中,相邻的字符串字符串被连接。如果两个字符串文字具有相同的编码前缀,则生成的连接字符串文字具有encoding-prefix。如果一个字符串文字没有编码前缀,则将其视为与其他操作数相同的编码前缀的字符串文字。

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 被正确推演为一个宽字符串文字。

Therefore, in your case, auto is correctly deduced as a wide string literal.

这篇关于在VC2015中连接不匹配的字符串WORKS - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆