Questions tagged [user-defined-literals]

User-defined literals are a C++ language feature (new in C++11) that allow the user to define new kinds of literal modifiers that will construct objects based on the string of characters that the literal modifies.

User-defined literals are a C++ language feature (new in C++11) that allow the user to define new kinds of literal modifiers that will construct objects based on the string of characters that the literal modifies.

Wikipedia entry: C++11 — User-defined literals

165 questions
12
votes
2 answers

Numeric literal operator error

Why does this code: constexpr float operator "" _deg(long double d) { // returns radians return d*3.1415926535/180; } static const float ANGLES[] = {-20_deg, -10_deg, 0_deg, 10_deg, 20_deg}; Produce 5 of these errors: error: unable to…
Eric
  • 95,302
  • 53
  • 242
  • 374
11
votes
1 answer

Why are non underscore names reserved to the implementation for UDL and not the other way around?

I know this is an old feature but reading on user defined literals e.g. return_t operator "" _a(long); // Literal operator for user-defined INTEGRAL literal I was reminded that the leading underscore is required. (Only the Standard Library is…
Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63
11
votes
1 answer

Why no standard-defined literal suffix for std::string?

A quick question: why doesn't C++11 offer a "user-" (really, standard library) defined literal for creating a std::string, such as auto str = "hello world"s; // str is a std::string Like C++, Objective-C supports both C-style strings and a more…
Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
11
votes
2 answers

How to automatically add literal definitions, based on a single user-defined literal?

C++11 offers user-defined literals. I've just started to play around with them, which made me wonder whether it would be possible to automatically add all SI multipliers to a single literal I define? For example, if I define Length operator ""…
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
11
votes
2 answers

How do I define a negative UDL in c++11 (are they disallowed?)?

I'm not even sure if negative User-Defined Literals are allowed. If not, why were they left out? For example, I would like to use: auto money_i_owe_jack = -52_jpy; This is what I tried using gcc 4.7.2: constexpr int64_t operator "" _jpy(long long…
kfmfe04
  • 14,936
  • 14
  • 74
  • 140
10
votes
4 answers

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…
deft_code
  • 57,255
  • 29
  • 141
  • 224
10
votes
2 answers

Is `using namespace std::literals` safe?

Generally, using namespace in global scope is considered as a bad practice. However, according to cppreference, the identifiers not starting with underscore(_) are reserved for standard library in the case of user-defined literals. the identifier…
ikh
  • 10,119
  • 1
  • 31
  • 70
10
votes
1 answer

Literal operator template: why not string?

Once again, while replying to another question, I forgot (my fault) that literal operator templates are picked up from the set of declarations only when integer or floating literals are found. As an example: template constexpr int…
skypjack
  • 49,335
  • 19
  • 95
  • 187
10
votes
1 answer

User-defined literals without underscore

According to cppreference it is possible to define a literal using CSomeClass operator ""s(const char* literal, size_t size); Now after reading the paragraph I think it should be also possible to define CSomeClass operator ""r(const char* literal,…
Uroc327
  • 1,379
  • 2
  • 10
  • 28
10
votes
2 answers

Can string-based user-defined literals be strongly typed?

The new user-defined literal concept in C++ suggests some very interesting uses of string-literals, such as: "Goodbye %s world"_fmt("cruel"); "Goodbye %s world"_fmt(123); // Error: arg 1 must be convertible to const char* R"(point = \((\d+),…
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
9
votes
1 answer

Should a using command issue a warning when using a reserved identifier?

When using the line using std::literals::chrono_literals::operator""s; in g++ 6.3.0, the compiler issues a warning stating: warning: literal operator suffixes not preceded by '_' are reserved for future standardization using…
9
votes
2 answers

Is there any way for compile-time check of string user-defined literal?

I'm writing a user-defined string literal to convert names of months into their numbers. The expected usage of this literal is something like "Nov"_m which should return 11. At the moment my code looks like constexpr Duration operator ""_m(const…
alexeykuzmin0
  • 6,344
  • 2
  • 28
  • 51
9
votes
1 answer

storage duration of underlying character data with user-defined string literal

Quick setup: I want to pass strings around in my program as a pointer and a size. I have a String class and a user-defined literal for constructing literal Strings: struct String { const char *ptr; size_t sz; }; inline constexpr String operator ""…
9
votes
2 answers

How to get smallest variable with C++11 user defined literals

I've been looking into some of the new features of C++11 and am very impressed with some of them, particularly the user defined literals. These allow you to define literals of the form 999_something where something controls what is done to the 999…
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
8
votes
1 answer

Reserved Names & User Literals

The C++ Standard reserves names beginning with an underscore followed by a capital letter in all scopes. Does this apply to user literal operators? e.g. int _MyInt; // reserved, violation template < char... > auto operator "" _MyInt ( ); //…
user13540365
1 2
3
10 11