user defined string literal, is string null terminated?
Asked Answered
U

1

8

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;
}
Unstudied answered 27/6, 2017 at 12:1 Comment(5)
Note: Your second overload is ill-formed I think, because the Standard only mandates that the template "shall have an empty parameter-declaration-clause and its template-parameter-list shall have a single template-parameter that is a non-type template parameter pack with element type char."Cocky
@Rakete1111: Is it ill formed to write: template <char...> void operator "" _d(){} "Hallo"_d;? This did not compile with g++. Did that mean it is impossible to use any templated form with user defined string literals?Unstudied
No, template<char...> type operator""_d() is the only allowed form. Every other use of templates is ill-formed. I don't know why it doesn't compile under g++, because it does so for me.Cocky
@Rakete1111: Yes, compiles for non-string user defined literals. But I wrote user defined string literals. Are you able to compile with "test"_d;?Unstudied
Yes sorry. I read your comment too quickly. You are right, there is no way to have user defined string literals.Cocky
U
6

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).

Unmindful answered 27/6, 2017 at 12:22 Comment(7)
You should mention that the second version is non-standard. [over.literal] "The declaration of a literal operator template shall have an empty parameter-declaration-clause and its template-parameter-list shall have a single template-parameter that is a non-type template parameter pack with element type char."Fredia
@Fredia Added mention.Unmindful
How could a conforming version of the function be written?Snavely
@rex There is no standard conforming way to write a string literal template.Unmindful
I've re-written the answer to be clearer about that.Unmindful
That the templated version is ill formed is new to me. Interesting that gcc did not complain about, also if --std=c++14 and -pedantic -Wextra is set!Unstudied
@Unstudied I suppose that the warning wasn't originally added since the implementers anticipated that it would become standard. Since C++14 was released without it, lack of warning is a GCC bug. Clang does warn when the extension is used.Unmindful

© 2022 - 2024 — McMap. All rights reserved.