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
8
votes
5 answers

User-defined literal string: compile-time length check

I have a user-defined literal operator that only makes sense for strings of a specific length, like this: constexpr uint16_t operator "" _int(const char* s, std::size_t len) { return len == 2 ? s[0] | (s[1] << 8) : throw; } This works: "AB"_int…
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
8
votes
1 answer

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…
Klaus
  • 24,205
  • 7
  • 58
  • 113
8
votes
1 answer

What is the scope of a user-defined literal?

Consider these two: namespace X1 { A operator "" _x(unsigned long long i) { return A{i}; } }; namespace X2 { B operator "" _x(unsigned long long i) { return B{i}; } }; The x literal is defined twice, but one of them is defined in namespace…
Sadeq
  • 7,795
  • 4
  • 34
  • 45
8
votes
3 answers

Are basic_string literals faster or handled better at compile-time?

While skimming over the draft of C++14/C++1y (n3690) I noticed the introduction of the basic_string litertal suffixes in section §21.7: inline namespace literals { inline namespace string_literals { // 21.7, suffix for basic_string literals: …
towi
  • 21,587
  • 28
  • 106
  • 187
8
votes
1 answer

Length of user-defined string literal as a template argument?

Is there any way to get behavior like this? // Some definition(s) of operator "" _my_str // Some definition of function or macro MY_STR_LEN using T1 = MY_STR_LEN("ape"_my_str); // T1 is std::integral_constant. using T2 =…
aschepler
  • 70,891
  • 9
  • 107
  • 161
7
votes
2 answers

Physical Boost.Units User Defined Literals

Now that we soon have user defined literals (UDL), in GCC 4.7 for example, I'm eagerly waiting for (physical) unit libraries (such as Boost.Units) using them to ease expression of literals such as 1+3i, 3m, 3meter or 13_meter. Has anybody written an…
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
7
votes
1 answer

Can a C++ user-defined literal operator ever be passed a null pointer?

Can a C++ user-defined literal operator ever be passed a null pointer? This is really happening with an experimental version of g++ (gcc version 4.7.0 20111114 (experimental) [trunk revision 181364] (Debian 20111114-1)) but I am unsure if this is a…
wjl
  • 7,519
  • 2
  • 32
  • 41
7
votes
2 answers

Do preprocessor defines substitute in `operator""_name`

Consider the following example provided by Aykhan Hagverdili: #include using std::operator""s; #define s foobar auto s = "hello world"s; Some compilers would substitute s and fail compilation. Some compilers would not substitute s. See…
Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
7
votes
3 answers

template parameter list for literal operator

Here is my implementation of converting binary literal to decimal: template constexpr int operator"" _b() { if constexpr (sizeof... (Tail) == 0) { return Head - '0'; } else { return (Head…
7
votes
0 answers

Is there a bug with parsing member access expressions on a user-defined integer literal in Clang and GCC?

The following code appears to be valid C++, accepted by all major compilers: #include #include auto main() -> int { using namespace std::string_literals; std::cout << "Hello"s.length(); } The following, however, is…
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
7
votes
1 answer

Integer sequence of chars from user-defined literal taking strings as parameters

Currently, only doubles can produce a template of chars in a user defined literal: template double operator "" _x(); // Later 1.3_x; // OK "1.3"_y; // C++14 does not allow a _y user- // defined operator to parse that as a template…
Vincent
  • 57,703
  • 61
  • 205
  • 388
7
votes
1 answer

Using user-defined literals in expressions sometimes requires whitespace

The following code compiles in both GCC and Clang: long double operator""_a(long double); auto x = 0e1_a+0; // OK But not this (replacing _a with _e): long double operator""_e(long double); auto y = 0e1_e+0; // Error: unable to find numeric…
T.C.
  • 133,968
  • 17
  • 288
  • 421
7
votes
1 answer

Integer overflow with UDL (user defined literal) for __int128 @ min negative value

For clarity and simplicity I will shorten the following numbers as follows: −170,141,183,460,469,231,731,687,303,715,884,105,728 as -170…728 170,141,183,460,469,231,731,687,303,715,884,105,727 as 170…727 These numbers represent the minimum and…
bolov
  • 72,283
  • 15
  • 145
  • 224
7
votes
1 answer

C++11 user-defined literals

I'm learning C++11, and am interested in user-defined literals. So I decided to play a bit with it. Some languages have a syntax like this: int n = 1000_000_000; I tried to simulate this feature in C++11. inline constexpr unsigned long long…
Khurshid Normuradov
  • 1,564
  • 1
  • 13
  • 20
6
votes
0 answers

Understanding user-defined string literal compiler error ".. is not a variable"

I'm wondering about the difference between these two string UDLs. While the first one is compiling nicely, I get an error with the second one. The only difference is that Literal1 uses std::array as storage, while Literal2 uses const char *. I can…
florestan
  • 4,405
  • 2
  • 14
  • 28