Concatenating mismatched string WORKS in VC2015 - How?

馋奶兔 提交于 2019-12-10 15:09:52

问题


When we have either of these:

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

Any pre-VS2015 compiler would raise error:

error C2308: concatenating mismatched strings

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?


回答1:


In C++03 this behaviour would be undefined.

ISO 14882-2003: 2.13.4.3 states that

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 states that

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.

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



来源:https://stackoverflow.com/questions/31809141/concatenating-mismatched-string-works-in-vc2015-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!