2015-08-04 5 views
5

Quando abbiamo uno di questi:Concatenazione di stringhe non corrispondenti WORKS in VC2015 - Come?

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

qualsiasi compilatore pre-VS2015 solleverebbe errore:

error C2308: concatenating mismatched strings

Ma con il compilatore VC2015, compila bene e il tipo risultante (auto deduzione) è un stringa wide-char.

La mia domanda è: quando e come questo è reso possibile - qualsiasi specifica standard?

+0

Questo potrebbe aiutare: http://en.cppreference.com/w/cpp/language/string_literal – Mithrandir

risposta

8

In C++ 03 questo comportamento non è definito.

ISO 14.882-2003: 2.13.4.3 afferma che

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.

Non so esattamente quando la modifica è stata introdotta, ma il comportamento è almeno ben definito nel progetto di N3242 dello standard.

ISO 14.882-2011: 2.14.5.13 afferma che

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.

Pertanto, nel tuo caso, auto sia correttamente dedotta come una vasta stringa letterale.