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;
}
Klaus
  • 24,205
  • 7
  • 58
  • 113
  • 2
    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."](http://eel.is/c++draft/over.oper#over.literal-5) – Rakete1111 Jun 27 '17 at 12:16
  • @Rakete1111: Is it ill formed to write: `template 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? – Klaus Jun 27 '17 at 14:01
  • 1
    No, `template 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. – Rakete1111 Jun 27 '17 at 14:05
  • @Rakete1111: Yes, compiles for non-string user defined literals. But I wrote user defined *string* literals. Are you able to compile with `"test"_d;`? – Klaus Jun 27 '17 at 14:14
  • Yes sorry. I read your comment too quickly. You are right, there is no way to have user defined *string* literals. – Rakete1111 Jun 27 '17 at 14:18

1 Answers1

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

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    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." – AndyG Jun 27 '17 at 12:27
  • @AndyG Added mention. – eerorika Jun 27 '17 at 12:32
  • How could a conforming version of the function be written? – wally Jun 27 '17 at 12:34
  • 1
    @rex There is no standard conforming way to write a string literal template. – eerorika Jun 27 '17 at 12:43
  • I've re-written the answer to be clearer about that. – eerorika Jun 27 '17 at 12:53
  • 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! – Klaus Jun 27 '17 at 13:47
  • @Klaus 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. – eerorika Jun 28 '17 at 01:39