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

150 questions
146
votes
18 answers

Conveniently Declaring Compile-Time Strings in C++

Being able to create and manipulate strings during compile-time in C++ has several useful applications. Although it is possible to create compile-time strings in C++, the process is very cumbersome, as the string needs to be declared as a variadic…
void-pointer
  • 12,317
  • 11
  • 40
  • 60
144
votes
12 answers

What new capabilities do user-defined literals add to C++?

C++11 introduces user-defined literals which will allow the introduction of new literal syntax based on existing literals (int, hex, string, float) so that any type will be able to have a literal presentation. Examples: // imaginary…
Motti
  • 99,411
  • 44
  • 178
  • 249
62
votes
4 answers

Advantages of using user-defined literal for strings instead of string literal

The strings topic in the SO Documentation used to say, in the Remarks section: Since C++14, instead of using "foo", it is recommended to use "foo"s, as s is a string literal, which converts the const char * "foo" to std::string "foo". The only…
Rakete1111
  • 42,521
  • 11
  • 108
  • 141
42
votes
3 answers

Are C++14 digit separators allowed in user defined literals?

While clang compiles the following line, g++ 6.1 complains about the digit separator (see live example on Coliru): auto time = 01'23s; Which compiler, if any, is correct according to the C++14 standard (N3796)? Otherwise, is allowing digit…
28
votes
2 answers

Does anyone have information on using operator""?

Bjarne Stroustrup gave a keynote presentation today for the Going Native 2012 conference. In his presentation, he discussed the issue of enforcing correct units. His elegant (IMHO) solution to this involved using an operator I have never heard of…
Joseph Buntic
  • 263
  • 2
  • 5
25
votes
1 answer

What is C++20's string literal operator template?

What is C++20's string literal operator template? Cppreference's example in this respect is quite concise and not very clear to me: struct A { A(const char *); auto operator<=>(const A&) const = default; }; template A operator ""_a(); In…
lukeg
  • 3,489
  • 3
  • 15
  • 37
23
votes
5 answers

User defined literal arguments are not constexpr?

I'm testing out user defined literals. I want to make _fac return the factorial of the number. Having it call a constexpr function works, however it doesn't let me do it with templates as the compiler complains that the arguments are not and cannot…
Pubby
  • 48,511
  • 12
  • 121
  • 172
22
votes
1 answer

Why do user-defined string literals and integer literals have different behavior?

I'm learning about user-defined literals, and confused with the following test code: std::chrono::seconds operator"" _s(unsigned long long s) { return std::chrono::seconds(s); } std::string operator"" _str(const char *s, std::size_t len) { …
for_stack
  • 14,584
  • 2
  • 20
  • 34
22
votes
4 answers

Why do I have to use long double for user defined literals?

The following user defined literal omits an error: constexpr double operator "" _kg(double q) { return q*1000; } but if long is added the error will disappear and code will work as follows: constexpr double operator "" _kg(long double q) { …
Sameh Kamal
  • 2,235
  • 4
  • 25
  • 55
20
votes
3 answers

Why aren't C++14 standard-defined literals in the global namespace by default?

C++14 includes standard-defined literals for, amongst other things, std::string and various timespans from the header. To use them you must say using namespace std::literals; (or some variation depending on exactly which literals you want,…
Tristan Brindle
  • 15,036
  • 2
  • 31
  • 79
19
votes
1 answer

Can I template user-defined literals?

Suppose I have some class: template class Foo { const T* x_; public: Foo(const T* str) : x_{str} {} }; and I provide some user-defined literals that create a Foo object: Foo operator"" _foo(const char* str, std::size_t) { …
Zorawar
  • 5,509
  • 2
  • 17
  • 40
16
votes
2 answers

Empty destructor vs literal destructor

Consider the following code: #include class Test { public: constexpr Test(const int x) : _x(x) {} constexpr int get() const {return _x;} ~Test() {} // HERE protected: const int _x; }; int…
Vincent
  • 50,257
  • 51
  • 171
  • 339
15
votes
1 answer

Using macro with string fails on VC 2015

Why does this fail to compile? char programDate[] = "("__DATE__")"; But this compiles fine (see space): char programDate[] = "(" __DATE__")"; I do know VC2015 now supports literal-operators. But shouldn't that be in compilation phase? __DATE__…
Ajay
  • 16,823
  • 9
  • 50
  • 94
15
votes
1 answer

String literal with dependent type — impossible?

Is it possible to define a user-defined string literal conversion operator such that the type of its result depends on the value of its string input? It is easy with user-defined integer and floating point literals because they admit literal…
n. 'pronouns' m.
  • 95,181
  • 13
  • 111
  • 206
15
votes
2 answers

Code I've never seen in C++11

I'm looking at this source code template struct conv2bin; template struct conv2bin { static_assert(high == '0' || high == '1', "no bin num!"); static int const value = (high - '0')…
1
2 3
9 10