Klaus
Klaus

Reputation: 25623

user defined string literal, is string null terminated?

For user defined string literals, is the given string guaranteed null terminated if I use the following form of definition? I know that the size given with second parameter count without any termination if there is any.

void operator"" _x( const char* n, size_t s)
{
    std::cout << "String: " << s << " Len: " << s << std::endl;
}

If I use this version of definition I see no null termination character!

template <class T, T... Chrs>
void operator""_s() 
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

Upvotes: 8

Views: 1014

Answers (1)

eerorika
eerorika

Reputation: 238401

user defined string literal, is string null terminated?

void operator"" _x( const char* n, size_t s)

Yes. String literals are null terminated and n points to such string literal.


If I use this version of definition I see no null termination character!

template <class T, T... Chrs>
void operator""_s()

The standard does not allow string literal templates. There is the document N3599 which proposes its addition to the standard, and it was intended for C++14 but there was no consensus and it hasn't become part of the standard yet. GCC and Clang at least appear to have already implemented it as a language extension.

Indeed, the literal operator template does not receive the null character as one of its arguments.

Proposal N3599:

the remaining arguments are the code units in the string literal (excluding its terminating null character).

Upvotes: 6

Related Questions