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

User-Defined Literal for Two-Argument Constructor

Please consider the following code: #include class Point { public: int x,y; Point(int newx, int newy) : x(newx), y(newy) {} }; Point operator"" x(const unsigned long long i) { return Point(i, 0); } int main() { Point p…
1.618
  • 1,765
  • 16
  • 26
4
votes
2 answers

How can variadic char template arguments from user defined literals be converted back into numeric types?

This question is being asked because of this one. C++11 allows you to define literals like this for numeric literals: template OutputType operator "" _suffix(); Which means that 503_suffix would become <'5','0','3'> This is nice, although…
Pubby
  • 51,882
  • 13
  • 139
  • 180
4
votes
4 answers

Can user defined literals have functions as arguments?

Can functions be used with user defined literals? If so, what shenanigans can be done? Is this legal? void operator "" _bar(int (*func)(int)) { func(1); } int foo(int x) { std::cout << x << std::endl; } int main() { foo(0); // print 0 …
Pubby
  • 51,882
  • 13
  • 139
  • 180
4
votes
2 answers

Should user defined literals always be consteval in C++20?

If I'm not mistaken, the arguments for a user defined literal are always known at compile time. In C++20 you can force functions to execute at compile time using consteval so that throw generates a compile time error. #include consteval…
4
votes
1 answer

How to define compile time ternary literal in C++?

In Chapter 19 of the 4th edition of the C++ Programming Language book, there is an example of defining a ternary number literal using a template technique, but the example does not compile. I tried to fix it in the way it looks right to me, but it…
bobeff
  • 3,543
  • 3
  • 34
  • 62
4
votes
1 answer

It is possible to pass an empty string literal to a user-defined raw literal operator?

Consider the following simple user-defined raw literal operator: #include std::string operator""_s(const char* s) { return s; } which gives us another easier way to generate std::strings with only integer characters: auto s1 = 012345_s;…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
4
votes
2 answers

Using operator ""s for std::chrono with gcc

I want to use the std::chrono::duration literals, such as 10s to mean "10 seconds", like this: std::chrono::duration millisecs = 10s; But, I'm getting this error: main.cpp:20:17: error: unable to find numeric literal operator…
Klaus
  • 24,205
  • 7
  • 58
  • 113
4
votes
1 answer

template alternative for user defined literals

I created a template class, and I wanted to use a user defined literals. My code: template class MyClass { // class code }; // template /* Not allowed */ MyClass<17> operator "" _G(const char* param, size_t length) { …
Shadi
  • 1,701
  • 2
  • 14
  • 27
4
votes
2 answers

User defined literals definitions

I was taking a look at the cppreference page for user defined literals, and I think I understand everything except a few examples template double operator "" _π(); // OK How does this operator work? How can you call it? double…
Curious
  • 20,870
  • 8
  • 61
  • 146
4
votes
3 answers

Compile time consistent hash of char types

I want to implement of sort of Symbol the same way ruby does. For this, I created a user defined literal which returned a std::hash of the std::basic_string corresponding. The code was great, but as I read somewhere the hash function may not be…
Jaffa
  • 12,442
  • 4
  • 49
  • 101
3
votes
1 answer

C++ user-defined string literal template weird issue (getting string literal length as compile-time constant)

I'm trying to define a user-defined string literal template. Shouldn't that code snippet work? / Why doesn't it work? template constexpr int operator ""_x () { return 0; } int x = "abc"_x; The operator declaration is 'accepted' by the…
grekd
  • 43
  • 4
3
votes
1 answer

User Defined Literals for a String versus for a Hex Value

Regarding this question, why does a a user defined literal for a hex value map to a different string literal operator than a string does? That is, why does the code std::vector val1 = 0x229597354972973aabbe7_hexvec; map…
Steve
  • 3,957
  • 2
  • 26
  • 50
3
votes
1 answer

Convert const char* to const char_type* at compile time

Consider the following code: using char_type = /*implementation defined*/; void foo(const char_type*); int main() { foo("Hello World!"); } The string literal "Hello World!" is a const char* that, depending on the implementation, may not be…
user7769147
  • 1,559
  • 9
  • 15
3
votes
1 answer

Two double quotes around preprocessor token in macro definition

I don't understand the significance of using two double quotes with strings in C++. I saw the following code somewhere: class str_literal; static str_literal operator"" _s(const char *s, size_t len); class str_literal { private: explicit…
nishantsingh
  • 4,537
  • 5
  • 25
  • 51
3
votes
1 answer

Universal number system conversion to decimal with udl

I'm trying to create a universal conversion function which aims to convert base-any numeral system to decimal: namespace detail { template constexpr auto toDecImpl() { return Chs > '9' ? Chs - 'A' + 10 : Chs - '0'; …
Chen Li
  • 4,824
  • 3
  • 28
  • 55