Skip to content

CPP-6594 S817 Add C++23 context and Unicode string literal examples #5138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions rules/S817/cfamily/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
== Why is this an issue?

Concatenation of wide and narrow string literals has not always been supported in C or {cpp}, and even when supported, the meaning may be unclear to the reader. Concatenation of string literals with different encodings is only conditionally supported, and may be removed in a future version of the language.

Concatenation of wide and narrow string literals has not always been supported in C or {cpp}, and even when supported, the meaning may be unclear to the reader. Furthermore, concatenation of string literals with different encodings was only conditionally supported prior to {cpp}23, and is now explicitly ill-formed as of {cpp}23.

Therefore, only string literals with the same prefix should be concatenated together.


=== Noncompliant code example

[source,cpp]
[source,cpp,diff-id=1,diff-type=noncompliant]
----
wchar_t n_array[] = "Hello" L"World"; // Noncompliant
wchar_t w_array[] = L"Hello" "World"; // Noncompliant
auto u8_array = "Hello" u8"World"; // Noncompliant
auto u_array = u"Hello" "World"; // Noncompliant

// Mixed encoding prefixes (ill-formed as of C++23)
auto mixed1 = L"Hello" u8"World"; // Noncompliant
auto mixed2 = u"Hello" U"World"; // Noncompliant
auto mixed3 = u8"Hello" u"World"; // Noncompliant
----


=== Compliant solution

[source,cpp]
[source,cpp,diff-id=1,diff-type=compliant]
----
char_t n_array[] = "Hello" "World"; // Compliant
wchar_t w_array[] = L"Hello" L"World"; // Compliant
char n_array[] = "Hello" "World"; // Compliant
wchar_t w_array[] = L"Hello" L"World"; // Compliant
auto u8_array = u8"Hello" u8"World"; // Compliant
auto u_array = u"Hello" u"World"; // Compliant
----


Expand Down