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
1
vote
2 answers

Implementing km/h and m/s with user-defined literals

I want to implement two user-defined literals, _kmh for kilometers per hour and _ms for meters per second. I already have two structs for that and the operator "" functions: constexpr KMH operator "" _ms(long double val) { return KMH…
gartenriese
  • 4,131
  • 6
  • 36
  • 60
1
vote
1 answer

Cannot define user defined literals

I was reading about user-defined literals, and I came across this snippet, but I got compiler error when I tried to use it. int operator ""_fix(long double d) { // returns d as a 1.15.16 fixed point number return (int)(d*65536.0f); } It…
The Vivandiere
  • 3,059
  • 3
  • 28
  • 50
1
vote
1 answer

C++11 recursive lambda function within constexpr operator""

while exploring the subject of constexpr/operator"' features of C++11 I stumbled upon this article: http://www.codeproject.com/Articles/447922/Application-of-Cplusplus11-User-Defined-Literals-t It quotes an example of how would a code providing…
1
vote
1 answer

c++11 user defined literals, conflicts with compilation/execution dichotomy

I know the ISO C standard make a big deal about separating translation behaviour and execution behaviour, partly to ensure cross-compilers don't have to carry the execution environment of every target. By that, I mean the compiler has limited…
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1
vote
2 answers

Overloading assignment operator for type deduction

Here's the ideone code: http://ideone.com/Qp8Eqg My question is, is it possible to force a conversion based on the lvalue alone? For example, [Seconds] s = 2_h + 60_s; cout <
user1508519
0
votes
1 answer

Why the move constructor nor the move assign operator is not called in this expression with UDL?

I have a class with copy constructor and copy assign operator deleted. Move constructor amd move-assign operator are present. It also defines an operator<<: struct S { S(std::size_t s) {} S(const S &) = delete; S(S &&) { std::cout <<…
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
0
votes
1 answer

Does c++14 literal operator only support long double and unsigned long long type?

I've tried this code: #include using namespace std; constexpr int operator"" _w(int d) { return d; } struct Watt { int d; Watt(int _d) : d(_d) {} }; Watt operator "" _watt(int d) { return Watt(d); } int main() { Watt w1 =…
Troskyvs
  • 7,537
  • 7
  • 47
  • 115
0
votes
3 answers

Numeric UDL operator template

I'm trying to define what Pablo Halpern calls a numeric UDL operator template. I want it to return a lambda that would count how many characters at the beginning of a char array are from the given set. Here's my test code: template
SU3
  • 5,064
  • 3
  • 35
  • 66
0
votes
0 answers

How to make small signed integer literals that take into account 2s complement?

I've been trying to make signed literal short hands for types, for example, u8, u16, etc... I developed a function like this: constexpr std::int8_t operator "" _i8(unsigned long long int value){ unsigned long long int mask = 0xEF; …
Krupip
  • 4,404
  • 2
  • 32
  • 54
0
votes
1 answer

User-defined literal for stringstream

I wrote an operator function that returns std::stringstream when suffix _f appears. #include #include #include static std::stringstream&& operator "" _f(const char* const s, const size_t _) { return…
sanitizedUser
  • 1,723
  • 3
  • 18
  • 33
0
votes
1 answer

User-defined literal fails to include another file

I have a class Komp where I have a user-defined literal constexpr Komp operator""_i(long double x) to produce a komp object from e.g Komp k = 1 + 5.0_i;. This works fine in komp.cpp's main(), however when I write the same exact statement in…
StijnVanDijk
  • 3
  • 1
  • 3
0
votes
1 answer

A problem with user-defined literal operator (in raw mode)

I am trying to define a literal operator in literal mode (i.e) the function parameter list should be const char* arg1 only not const char* arg1,size_t size but I can't #include #include int operator"" _i(const char*…
asmmo
  • 6,922
  • 1
  • 11
  • 25
0
votes
1 answer

User-defined literal operator isn't recognised in my class

I have written a class that uses literal operators to indicate a measurement error to be used in physical applications. I have defined a literal ""_err that takes long double as an argument and returns a struct called Error. For instance, 1.3_err. A…
0
votes
0 answers

c++, user-defined literal operator not found in struct

in a c++ project I have two header files. in the first header file there is a struct that I want to instantiate as an array in the second file. at the initialization of the first member at the second index of array I get the error. h1.h …
Amir-Mousavi
  • 4,273
  • 12
  • 70
  • 123
0
votes
1 answer

Can I invoke at run-time the logic for choosing which user-defined literal to call?

In my C++ program I've got these three user-defined literal operators: constexpr Duration operator""_h(unsigned long long int count) { return {count / 1.f, true}; } constexpr Duration operator""_m(unsigned long long int count) { return {count /…
mjwach
  • 1,174
  • 2
  • 9
  • 25
1 2 3
10
11