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
163
votes
20 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
  • 14,247
  • 11
  • 43
  • 61
147
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
  • 110,860
  • 49
  • 189
  • 262
66
votes
5 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
  • 47,013
  • 16
  • 123
  • 162
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…
31
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
  • 4,189
  • 3
  • 19
  • 40
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
24
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
  • 21,012
  • 4
  • 35
  • 48
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
  • 51,882
  • 13
  • 139
  • 180
23
votes
4 answers

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

The following user defined literal emits an error: constexpr double operator "" _kg(double q) { return q*1000; } but if long is added the error will disappear and the code will work as follows: constexpr double operator "" _kg(long double q) { …
Sameh K. Mohamed
  • 2,323
  • 4
  • 29
  • 55
22
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
  • 6,505
  • 2
  • 23
  • 41
20
votes
1 answer

How to refer to user defined literal operator inside a namespace?

Consider the following: #include namespace X { void operator ""_test(unsigned long long x) { std::cout << x; } } int main() { using namespace X; 10_test; // 10_X::test; /* doesn't work */ } I can refer…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
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
  • 16,281
  • 4
  • 39
  • 82
16
votes
1 answer

Fixed-width integer literals in C++?

C++11 first introduced support for defining new literals into C++ by means of user-defined literals. Does C++11 or later also predefine suffixes for fixed-width integer literals for types in ?
jotik
  • 17,044
  • 13
  • 58
  • 123
16
votes
2 answers

Is it possible to disable GCC warning about missing underscore in user defined literal?

void operator"" test( const char* str, size_t sz ) { std::cout<
cmeub
  • 497
  • 2
  • 11
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
  • 57,703
  • 61
  • 205
  • 388
1
2 3
10 11