Search code examples
c++c++14string-literalsc++17rawstring

Why must the delimiters of raw string literals be under 16 chars?


The following program does not compile:

#include <iostream>

int main() {
    std::cout << R"RAW_STRING_LITERAL(
        hello
        world
        )RAW_STRING_LITERAL";
}

error: raw string delimiter longer than 16 characters.

Why is there a limitation length imposed on raw string delimiters?


Solution

  • The earliest proposal I can find for raw string literals is N2146 by Beman Dawes. It contains the text:

    The maximum length of d-char-sequence shall be 16 characters.

    This seems to be an arbitrary limit imposed by the author, who probably decided 16 characters were sufficient for creating an unambiguous delimiter sequence in all cases.

    The proposal also states

    The terminating d-char-sequence of a raw string literal shall be the same sequence of characters as the initial d-char-sequence

    So a conforming implementation must buffer and process the d-char-sequence to ensure the two sequences match. The absence of any limit on the d-char-sequence would unnecessarily add to the complexity of implementing the feature.