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

noexcept constructor of std::string_view

According to the documentation, std::string_view has a constructor that takes a const char * and a std::size_t, that is not declared noexcept: constexpr basic_string_view(const CharT* s, size_type count); On the other hand, the documentation states…
Giovanni Cerretani
  • 1,693
  • 1
  • 16
  • 30
6
votes
1 answer

Does C++17 allow a non-ascii character as an identifier?

At cppref, I find a strange C++ code, which uses a non-ascii character in source code as follows: template double operator "" _π(); // OK However, the code above cannot be cmompiled with clang 6.0. The error message is: error : source…
szxwpmj
  • 465
  • 2
  • 10
6
votes
1 answer

`using` declaration for a user-defined literal operator

Is it possible to have a using declaration for the literals operator, operator ""? E.g., #include namespace MyNamespace { constexpr std::chrono::hours operator "" _hr(unsigned long long n){ return std::chrono::hours{n}; } // ...…
Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
6
votes
1 answer

User-defined literal to MPL sequence: is this legal?

Being able to convert the string passed to a literal operator into an MPL sequence would be useful, since we would then be able to control code generation based on the contents of the string. Previously, I thought this to be impossible, since the…
6
votes
1 answer

How to declare friend user-defined literal operator within template class?

It is unclear why the code below does not compile with GCC g++ 4.7 telling the following: $ g++ -std=c++11 -fPIC test.cpp test.cpp:11:45: error: ‘B operator"" _b(const char*, size_t)’ has invalid argument list If class C is declared non-template…
dzidzitop
  • 385
  • 2
  • 11
6
votes
2 answers

Combining a user defined literal with a method call

I'm wondering why I can't write code like this: constexpr double radius = 27_km.to_miles(); // _km returns Distance instance // which has to_miles() Both GCC 4.8.1 and Clang 3.4 complain that they…
Alex Korban
  • 14,916
  • 5
  • 44
  • 55
6
votes
1 answer

C++ available "literal suffix code" for units

C++1x supports literal suffixes (cmp. e.g. http://ecn.channel9.msdn.com/events/GoingNative12/GN12Cpp11Style.pdf). I am using gcc 4.7 and want to introduce some units for our system. Most notably half of our code uses degrees and the other half…
Frankie
  • 653
  • 1
  • 9
  • 20
6
votes
2 answers

User-Defined String Literals Vs. Other User-Defined Literals

Let's consider the following quote from the C++11 standard (the N3376 draft, to be precise): (2.14.8.5) If L is a user-defined-string-literal, let str be the literal without its ud-suffix and let len be the number of code units in str (i.e.,…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
6
votes
1 answer

Where can I find a listing of C++11 type prefixes/suffixes?

Could somebody point me to a complete listing of language type prefixes/suffixes? prefix examples: auto s1 (u8"I'm a UTF-8 string."); auto s2 (u"This is a UTF-16 string."); auto s3 (U"This is a UTF-32 string."); auto s4 (R"(RAW \ STRING " )"); auto…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
5
votes
3 answers

C++0x, user-defined literals with friend operator ""()

Will it be possible and/or useful to define an operator "" (...) as a friend function? class Puzzle { friend Puzzle operator "" _puzzle(const char*, size_t); ... }; void solve(Puzzle); int main() { solve("oxo,xox"_puzzle); }; I am thinking…
towi
  • 21,587
  • 28
  • 106
  • 187
5
votes
1 answer

What does the operator"" do in C++?

How do you call this operator? Can you use it for things other than the creation of custom literals? Example usage: (see cppreference) constexpr long double operator"" _deg ( long double deg ) { return deg * 3.14159265358979323846264L / 180; }
User12547645
  • 6,955
  • 3
  • 38
  • 69
5
votes
1 answer

chrono literals in VS2015

The following code gives me a compile time error: #include int main() { auto day = 24h; return 0; } Error C3688: invalid literal suffix 'h'; literal operator or literal operator template 'operator ""h' not found. I'm trying this…
Voo
  • 29,040
  • 11
  • 82
  • 156
5
votes
0 answers

Is there a reason to place operator ""s into std::literals::string_literals?

I'm pretty surprised to discover that this code doesn't compile (assuming we're using a C++14 compiler): std::cout << "hello world!\n"s; The error showed by ideone is the following: unable to find string literal operator 'operator""s' Luckily is…
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
5
votes
1 answer

Custom literal works with long double but not double, and with pass by value but not pass by reference

I'm experimenting with C++ custom literals. I'm finding it strange that the simple function below stops working when I change the type from long double type to double, or when try to pass by reference. At first I thought it had to do with the use…
MGA
  • 1,658
  • 15
  • 28
5
votes
1 answer

C++ 11 User Defined Literals with Microsoft Visual Studio 2013

As far as I know UDL are included and supported in MVS 2013. I have tried to do things such as: myclass operator"" _suffix(); int operator"" _suffix(); Both the above lines give errors at "" saying it expected an operator. My guess is…
taigi tanaka
  • 299
  • 1
  • 4
  • 17