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
3
votes
2 answers

How to place user-defined literal inside constexpr class of same type in C++?

I want to implement my own string-literal class Literal and operator: constexpr Literal operator""_s(const char* str, size_t size); class Literal { friend constexpr Literal operator"" _s(const char*, size_t); constexpr Literal(const char* str,…
abyss.7
  • 13,882
  • 11
  • 56
  • 100
3
votes
2 answers

Invoke user-defined literal on lvalue

Is there any way to invoke a user defined literal on lvalues? e.g I would like to int operator "" _xor1(int a) { return a^1; } // Works fine 17_xor1; auto myint = get_something_only_availabe_at_runtime(); // Any way to use _xor1 on…
3
votes
2 answers

User defined const char* literal example from cppreference

On cppreference there is this example (http://en.cppreference.com/w/cpp/language/user_literal): void operator"" _print ( const char* str ) { std::cout << str; } int main(){ 0x123ABC_print; } Output: 0x123ABC And I fail to understand…
Michael Mahn
  • 737
  • 4
  • 11
3
votes
0 answers

Can I call a method directly on the result of user-defined literal?

I tried invoking a method directly on the result of the user-defined literal, but my results varied depending on the compiler. #include std::string operator ""_test(unsigned long long value) { return std::to_string(value); } int…
sthlm58
  • 1,142
  • 1
  • 9
  • 28
3
votes
1 answer

user-defined literals overloading in

chrono takes user-defined literals to make it more convenient. For each literal, it has two overloading functions. constexpr chrono::hours operator "" h(unsigned long long); constexpr chrono::duration> operator "" h(long…
for_stack
  • 21,012
  • 4
  • 35
  • 48
3
votes
1 answer

Using macros in printf function in VS2013 vs VS2017

I have defined this macro in my source code #define UINT_08X_FORMAT "%08X" I need to use the above in printf like this: printf("Test - "UINT_08X_FORMAT"", 50); It compiles and works fine in VS2013 where as in VS2017, it throws the following…
Arun
  • 2,247
  • 3
  • 28
  • 51
3
votes
1 answer

Non-string literals are prvalues?

I'm not sure if I'm missing something, but, user-defined literals, which invokes user-defined functions that could return anything, are also a kind of literals. The standard says that a literal is always a prvalue, unless it is a string literal,…
ABu
  • 10,423
  • 6
  • 52
  • 103
3
votes
1 answer

Can I use a namespace::user-defined-literals directly in an expression

Is it possible to write a user defined literal without writing using namespace xxx;. Something like ::; For instance namespace tostr { std::string operator "" _UP(const char *str, unsigned long long int) { …
Jacinto Resende
  • 343
  • 2
  • 13
3
votes
2 answers

Using UDL in header outside function scope

If we want to use some UDL we need to use the corresponding namespace: auto foo() { using namespace std::literals::chrono_literals; std::chrono::milliseconds interval = 1s; } which is all right and well because the introduced namespace is…
bolov
  • 72,283
  • 15
  • 145
  • 224
3
votes
2 answers

How do user-defined literals play together with digit separator?

I was just modifying an old example of my code by adding a digit separator to a user-defined literal, parsed by a variadic template: namespace lits { // helper for 1 arg template int bin(); // common template<> int bin<'1'>() {…
towi
  • 21,587
  • 28
  • 106
  • 187
3
votes
3 answers

User-defined literals (Extended literals) of C++11... which compilers support it?

In another thread I introduced some techniques we would use for Model-Driven-Development in C++ once C++11 features, in particular user-defined literals, are available. I just revised the plans for GCC 4.5 and even 4.6 and it shows that this…
Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
3
votes
2 answers

How can I define custom literals?

I am converting some source code from one scripting language (PAWN) to a programming language (C++) on Windows. The source code has millions of binary literals all over the place in the form of: data[] = { 0b11111111111011111110110111111110,…
Gizmo
  • 1,990
  • 1
  • 24
  • 50
2
votes
2 answers

What is the proper definition for a constexpr function that take a character array?

I'm writing a hashing function to help speed up string comparisons. My codebase compares strings against a lot of const char[] constants, and it would be ideal if I could work with hashes instead. I went ahead and translated xxHash to modern C++,…
2
votes
2 answers

Unsigned user-defined integer literal

Suppose I want to define an integer literal which also allows for negative values, e.g. -12_km. I.e., I would like to do using coord_t = long long; coord_t operator "" _km(long long int); However, this is not accepted by my compiler (gcc). The…
2
votes
1 answer

understanding user defined string literals addition for c++20

I found in user defined string literal the following: For user-defined string literals, let str be the literal without ud-suffix: a) If the overload set includes a string literal operator template with a non-type template parameter for which…
Klaus
  • 24,205
  • 7
  • 58
  • 113