Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating mismatched string WORKS in VC2015 - How?

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?

like image 506
Ajay Avatar asked Aug 04 '15 12:08

Ajay


1 Answers

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.

like image 100
ctor Avatar answered Oct 21 '22 14:10

ctor