Search code examples
c++language-lawyerc++23

Does C++ guarantee a minimum string length for string literals?


In C++ I can have string literals with a custom separator, like R"str-end(...verylong...)str-end";. I was astonished when Visual Studio spit out Compiler Error C2026, "string too big, trailing characters truncated", which happens when

The string was longer than the limit of 16380 single-byte characters.

Does C++ guarantee a minimum length that strings can have?

I looked up [lex.string] in C++ N4928 but didn't find anything. If nothing is specified, I could simply write a standard compliant compiler which gives C2026 for every string. That's not the idea, I presume.

Annex B, [implimits], only has a recommendation for the concatenated length, but not for an individual string.

The bracketed number following each quantity is recommended as the minimum for that quantity. However, these quantities are only guidelines and do not determine compliance.

(2.16) -- Characters in a string-literal (5.13.5) (after concatenation (5.2)) [65 536].

I am not looking for string::max_size, but my imagination was that I could use such a long string in my code.


Solution

  • The standard has no normative requirements in this area. It recommends that implementations live allow for strings up to a particular length, but it doesn't impose any requirements.

    Note also that MSVC's limitation is specifically on pre-concatenated string lengths. Essentially, this is the number of bytes that the compiler will let you store between quotes.

    If you want to store bulk binary data (as a string), you'll have to use something else.

    If nothing is specified, I could simply write a standard compliant compiler which gives C2026 for every string.

    Yes you could. And how many people would use your implementation?

    The standard doesn't need to forbid malicious or useless implementations; the market will do that.