Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which user-defined-literals are predefined by the standard?

My question sounds like a contradiction, but I don't know how else to refer to the new literal syntax other than user-defined-literal.

std::string operator "" s ( const char* str, size_t len )
{
   return std::string( str, len );
}

assert( "foo"s == "bar"s );

I remember hearing that user defined literals should start with an _ prefix. That would imply that the library defines some non-prefixed literals for us.

Does the standard provide some UDLs in the the standard library?
If yes, what are they?

like image 495
deft_code Avatar asked Mar 21 '11 16:03

deft_code


People also ask

What are user-defined literals?

In a raw user-defined literal, the operator that you define accepts the literal as a sequence of char values. It's up to you to interpret that sequence as a number or string or other type. In the list of operators shown earlier in this page, _r and _t can be used to define raw literals: C++ Copy.

What is a user-defined literal C++?

A literal is used for representing a fixed value in a program. A literal could be anything in a code like a, b, c2. , 'ACB', etc. Similarly, User-Defined Literals (UDL) provides literals for a variety of built-in types that are limited to integer, character, floating-point, string, boolean, and pointer.

What is string literal operator?

A string literal or anonymous string is a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally "bracketed delimiters", as in x = "foo" , where "foo" is a string literal with value foo .


2 Answers

The standard library actually defines no user defined literals. We would perhaps have expected complex numbers, but no.

On the other hand, there is also a proposal to remove them again

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3250.html

so we don't yet know what happens.

like image 117
Bo Persson Avatar answered Oct 03 '22 10:10

Bo Persson


The language already use regular literals suffixes, for example 1U.

It would become ambiguous if you were to use U as a user-defined-literal, thus the recommendation.

integer-suffix: u, U, l, L, ll, LL

floating-suffix: f, F, l, L

like image 43
Matthieu M. Avatar answered Oct 03 '22 08:10

Matthieu M.