Trevor Hickey
Trevor Hickey

Reputation: 37914

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?

Upvotes: 15

Views: 3235

Answers (2)

Praetorian
Praetorian

Reputation: 109219

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.

Upvotes: 13

ventsyv
ventsyv

Reputation: 3532

The standard specifies that:

A string-literal that has an R in the prefix is a raw string literal. The d-char-sequence serves as a delimiter. The terminating d-char-sequence of a raw-string is the same sequence of characters as the initial d-charsequence. A d-char-sequence shall consist of at most 16 characters

http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4527.pdf § 2.13.5 page 28

No reason is given in the standard but to me, this appear as completely random limit as it should make absolutely no difference what the delimiter is.

Upvotes: 1

Related Questions