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

Why does std::literals operators not automatically export when include their correspond header?

There are some literal operators in the std::literals namespace with their corresponding header such as operator""s, operator""sv etc. I wonder why those operators are not automatically export when including the corresponding header since they…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
1
vote
0 answers

GCC support for Compile-time string literals

C++20 introduces Class Types in Non-Type Template Parameters. As mentioned in the proposal, one of the motivations was the compile-time strings, and it enables to do this: template < size_t size > struct Literal : std::array { constexpr…
Vahagn
  • 4,670
  • 9
  • 43
  • 72
1
vote
0 answers

[over.literal]/1 doesn't seem to be accurate

Definition of literal-operator-id: literal-operator-id:     operator string-literal identifier     operator user-defined-string-literal [over.literal]/1 The string-literal or user-defined-string-literal in a literal-operator-id shall have no…
Alexander
  • 2,581
  • 11
  • 17
1
vote
1 answer

How to compose stringification with user defined literal (UDL) in Macro?

How to use literal suffix for identifier transformed into literal string in MACRO by #identifier? struct SomeType; SomeType operator "" _udl(const char* self); #define STRINGIFY_AS_UDL(id) /* #id _udl doesn't work */ /* How to have "id"_udl */…
Jarod42
  • 203,559
  • 14
  • 181
  • 302
1
vote
4 answers

User-defined Literals suffix, with *_digit..."?

A user-defined literal suffix in C++0x should be an identifier that starts with _ (underscore) (17.6.4.3.5) should not begin with _ followed by uppercase letter (17.6.4.3.2) Each name that [...] begins with an underscore followed by an uppercase…
towi
  • 21,587
  • 28
  • 106
  • 187
1
vote
3 answers

Is this string addition a valid expression?

I was curious if the compound assignment operators are valid for multiple parameters. My guess is += will have no side-effect but may not be the same case with "-=". std::string a; a += "Hello" + " "+"World"; // doesn't compile std::string name =…
1
vote
0 answers

Alias for user defined literal, i.e. 'using NS::operator ""_suffix' with another name

Consider that some library defines: namespace NS { constexpr atype operator ""_suffix(const char*, std::size_t); }; If I don't like the name _suffix (maybe it clashes with some other library, or it is in stark contrast with my naming…
Kijewski
  • 25,517
  • 12
  • 101
  • 143
1
vote
1 answer

Any idea why this user defined literals fails?

I have the following code which implements the adler32 checksum: constexpr uint32_t adler32(std::string_view sv) { constexpr const uint32_t MOD_ADLER= 65521; uint32_t rv= 0, a= 1, b= 0; for (unsigned char c:sv) { a=…
Michael Surette
  • 701
  • 1
  • 4
  • 12
1
vote
2 answers

JSON for Modern C++ _json Syntax

JSON for Modern C++ uses the following syntax: json j = "{ \"happy\": true, \"pi\": 3.141 }"_json; and I was wondering how they are accomplishing this. I don't understand the string literal _ json syntax.
sheridp
  • 1,386
  • 1
  • 11
  • 24
1
vote
3 answers

creating a literal for a Point - C++

I am trying to do a simple library where the object is a point on the xy-axis. I want to be able to use literals like this: Point a = (3,4); where (3,4) is a point literal. I read about user defined literals, but (as I understood) this seems to be…
Kamal Zidan
  • 474
  • 2
  • 9
1
vote
0 answers

Invalid literal suffix in MSVC 2017

I have the following code in a file called Console.hpp: inline std::wstring operator "" _t(const char* s) { std::wstring_convert> converter; std::wstring wide = converter.from_bytes(s); return…
Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166
1
vote
2 answers

Why doesn't this template signature work as a string-literal using quotes?

C++11 permits user-defined template string-literal operators, with the following template signature (taken from CppReference; the return type does not need to be double): template double operator "" _x(); However, it appears that this…
Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
1
vote
1 answer

Assign multibyte ASCII literals to enum values

One can assign an ASCII literal (can't call it a string) to enum value as following: #include // Macro to handle BIG/LITTLE ENDIAN // Endianness is suppoesed to handled in this macro #define TEMP(X) X enum t { XX = 'AA', // 0x4141 …
g-217
  • 2,069
  • 18
  • 33
1
vote
1 answer

Compile time encryption for strings using user-defined literals

I am aware that the new C++ standard allows for user-defined literals and that their generation can be done in compile time. However, I am quite new to the whole template metaprogramming universe and I'm trying to get some examples going, but still…
pabloxrl
  • 285
  • 2
  • 12
1
vote
2 answers

Is it possible to make the C++11 User-defined literals to be strip out?

Suppose I have a user-defined literal which would be used to calculate the hash code of a char[] at the compiling time: constexpr unsigned operator "" _hash(const char * pStr, std::size_t length) { // Some code here } And I could use it…
jayatubi
  • 1,972
  • 1
  • 21
  • 51