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
2
votes
1 answer

Is there a workaround to define a user-defined literal for shorts in c++?

I would like to define a user-defined literal for shorts. Just like this: short operator"" _s(int x) { return (short) x; } In order to define a short like this: auto PositiveShort = 42_s; auto NegativeShort = -42_s; However, as explained…
BlueTune
  • 1,023
  • 6
  • 18
2
votes
4 answers

C++: setting time using suffixes

Tell me, Can the following exist in C ++ 11/14/17: 1) set time using time suffixes double time1 = 1s; // time1 = 1.0 double time2 = 2m; // time2 = 120.0 double time3 = 7ms; // time3 = 0.007 2) get the string value of the time with the suffix as…
Zhihar
  • 1,306
  • 1
  • 22
  • 45
2
votes
1 answer

user-defined literals combined with an uint64_t argument

I just stumbled into the following user-defined literal: #include constexpr auto operator""_G(uint64_t v) { return v * 1'000'000'000ULL; } However, this does not compile with GNU 7.3.0 and -std=c++14. I get an "has invalid argument list"…
Raist246
  • 49
  • 4
2
votes
0 answers

How to unpack const char* str into template?

Motivation: Here is the implementation of converting base-n to decimal: template constexpr int toDec() { int ret{}; return ( (ret *= from, ret += Chs > '9' ? Chs - 'A' + 10 : Chs -…
Chen Li
  • 4,824
  • 3
  • 28
  • 55
2
votes
2 answers

User Defined String Literals compared to Const Strings

Consider the following code with these two structures: std::string operator"" _str(const char* str, std::size_t len) { return std::string( str, len ); } struct MessageLiterals { std::string HELP = "Press F1 for help"_str; std::string…
2
votes
1 answer

Overloading rules for User-defined-literals in c++0x

I am a little confused about overloading rules, let's say there are following literal operators, unsigned long long operator "" _xx(unsigned long long cooked_literal_int); //1 unsigned long long operator "" _xx(const char * raw_literal_string); //…
user534498
  • 3,926
  • 5
  • 27
  • 52
2
votes
2 answers

c++11 user defined literals for units of physical properties

I am trying to learn how to use c++11 user defined literals for units of physical properties. The question is, how do I avoid a mixing of these units. So that (8.0_kg + 8.0_km)--> gives error. any ideas guys? i am new to c++, be kind. class Mass{ …
WOT
  • 33
  • 1
  • 4
2
votes
0 answers

How to use user-defined literals for complex numbers?

What am I doing wrong when I try to execute std::pow(1.0i, 2)? Is it my understanding of user-defined literal operator""i, or is it how I use the complex pow-overload? The last line shows an error when compiling with g++-6.2 or g++-5: #include…
towi
  • 21,587
  • 28
  • 106
  • 187
2
votes
1 answer

what's wrong with following pack expansion?

constexpr int ipow(int x, int n) { return (n > 0) ? x * ipow(x, n - 1): 1; } template constexpr int b3_helper() { static_assert(c < '3', "not a ternary digit"); return c - '0'; } template constexpr int…
Sherwin
  • 847
  • 1
  • 6
  • 16
2
votes
1 answer

Literals and constexpr functions, compile-time evaluation

Attempting to implement a pleasing (simple, straightforward, no TMP, no macros, no unreadable convoluted code, no weird syntax when using it) compile-time hash via user-defined literals, I found that apparently GCC's understanding of what's a…
Damon
  • 67,688
  • 20
  • 135
  • 185
2
votes
1 answer

Should integer values be passed to floating point user defined literals?

I'm playing around with user-defined-literals (with GCC 4.7). double operator"" _lb(long double n) { return n * 0.453592; // convert pounds to kilos } This works fine when passing it a floating point literal (e.g. 42.0_lb) however when I try to…
Motti
  • 110,860
  • 49
  • 189
  • 262
1
vote
2 answers

Literals don't work for preprocessor macros

Literals don't seem to interplay well with preprocessor macros. For example, I have this preprocessor definition CONFIG_FADE_DELAY_MS that I want to translate to std::chrono::milliseconds. But the ms literal needs to be written close up to it, the…
glades
  • 3,778
  • 1
  • 12
  • 34
1
vote
1 answer

What is wrong with operator"" _Bq?

At [over.literal] I read, in the list of examples, that double operator""_Bq(long double); is valid, whereas double operator"" _Bq(long double); is ill-formed, which is apparently a consequence of the space right after "". Now, from the linked…
Enlico
  • 23,259
  • 6
  • 48
  • 102
1
vote
1 answer

User-defined literal doesn't work when the body is written in a different .cpp file

I created a user-defined literal like this, in is OWN .cpp file (declared as a friend function in .h file): fraction operator"" _frac(const long double val) { return fraction(static_cast(val)); } But in main it produces this error: Error…
1
vote
0 answers

Invalid suffix on floating constant

I created a user-defined literal like this: friend fraction operator "" _frac(long double val) { return fraction((float)val); } (Yes I know converting from long double to float is not good but I just do this for testing first and then fix…
KhiemGOM
  • 69
  • 8