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.
166
questions
0
votes
0
answers
80
views
Why do user defined literal operators have to start with underscore?
Since the Standard Library never defines literal operators in the root std namespace, what is the danger of having user defined operators not starting with underscore?
3
votes
1
answer
143
views
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<char...>
constexpr
int operator ""_x () {
return 0;
}
...
7
votes
2
answers
387
views
Do preprocessor defines substitute in `operator""_name`
Consider the following example provided by Aykhan Hagverdili:
#include <string>
using std::operator""s;
#define s foobar
auto s = "hello world"s;
Some compilers would ...
0
votes
1
answer
40
views
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) {}
...
5
votes
2
answers
234
views
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 ...
1
vote
2
answers
114
views
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 ...
1
vote
1
answer
222
views
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 ...
2
votes
2
answers
97
views
What is the proper definition for a constexpr function that take a character array?
I'm writing a hashing function to help speed up string comparisons.
My codebase compares strings against a lot of const char[] constants, and it would be ideal if I could work with hashes instead. I ...
0
votes
1
answer
285
views
Does c++14 literal operator only support long double and unsigned long long type?
I've tried this code:
#include <iostream>
using namespace std;
constexpr int operator"" _w(int d) { return d; }
struct Watt {
int d;
Watt(int _d) : d(_d) {}
};
Watt operator &...
4
votes
1
answer
75
views
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 ...
0
votes
3
answers
289
views
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....
-2
votes
1
answer
444
views
User-defined literals
In "User-defined literals" on cppreference.com, what does it mean by this?
b) otherwise, the overload set must include either, but not both, a raw literal operator or a numeric literal ...
2
votes
1
answer
100
views
How to make small signed integer literals that take into account 2s complement?
I've been trying to make signed literal short hands for <cstdint> types, for example, u8, u16, etc...
I developed a function like this:
constexpr std::int8_t operator "" _i8(unsigned ...
0
votes
1
answer
106
views
User-defined literal for stringstream
I wrote an operator function that returns std::stringstream when suffix _f appears.
#include <iostream>
#include <sstream>
#include <utility>
static std::stringstream&& ...
1
vote
1
answer
222
views
User-defined literal doesn't work when the body is written in a different .cpp file [duplicate]
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&...
1
vote
0
answers
63
views
Invalid suffix on floating constant [duplicate]
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 ...
4
votes
1
answer
205
views
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 <string>
std::string operator""_s(const char* s) {
return s;
}
which gives us another easier way to ...
1
vote
1
answer
208
views
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 ...
6
votes
0
answers
66
views
Understanding user-defined string literal compiler error ".. is not a variable" [duplicate]
I'm wondering about the difference between these two string UDLs. While the first one is compiling nicely, I get an error with the second one.
The only difference is that Literal1 uses std::array as ...
2
votes
2
answers
327
views
Unsigned user-defined integer literal
Suppose I want to define an integer literal which also allows for negative values, e.g. -12_km.
I.e., I would like to do
using coord_t = long long;
coord_t operator "" _km(long long int);
...
5
votes
1
answer
260
views
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 ...
3
votes
1
answer
252
views
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<uint8_t> val1 = ...
0
votes
1
answer
67
views
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(),...
2
votes
1
answer
1k
views
understanding user defined string literals addition for c++20
I found in user defined string literal the following:
For user-defined string literals, let str be the literal without ud-suffix:
a) If the overload set includes a string literal operator template ...
1
vote
0
answers
393
views
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 &...
4
votes
1
answer
400
views
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!" ...
1
vote
0
answers
74
views
[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
...
8
votes
1
answer
235
views
Reserved Names & User Literals
The C++ Standard reserves names beginning with an underscore followed by a capital letter in all scopes.
Does this apply to user literal operators?
e.g.
int _MyInt; // reserved, violation
template ...
6
votes
1
answer
685
views
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 ...
0
votes
1
answer
491
views
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<iostream>
#...
2
votes
1
answer
178
views
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;
...
3
votes
1
answer
1k
views
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);
...
1
vote
1
answer
137
views
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 ...
0
votes
1
answer
612
views
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 ...
3
votes
4
answers
3k
views
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 ...
3
votes
1
answer
197
views
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<char Chs> constexpr auto
toDecImpl()
{
...
1
vote
3
answers
80
views
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" + " "...
-1
votes
2
answers
523
views
Can a string literal be passed to a function that takes const char*?
I need help understanding some code.
I have read in other places that passing a string literal as a const char* is legal. But, in the last line of this code from cppreference for user-defined string ...
1
vote
0
answers
244
views
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 ...
2
votes
1
answer
669
views
user-defined literals combined with an uint64_t argument
I just stumbled into the following user-defined literal:
#include <cstdint>
constexpr auto operator""_G(uint64_t v) { return v * 1'000'000'000ULL; }
However, this does not compile with GNU 7....
31
votes
1
answer
8k
views
What is C++20's string literal operator template?
What is C++20's string literal operator template? Cppreference's example in this respect is quite concise and not very clear to me:
struct A { A(const char *); auto operator<=>(const A&) ...
15
votes
3
answers
2k
views
Using a C++ user-defined literal to initialise an array
I have a bunch of test vectors, presented in the form of hexadecimal strings:
MSG: 6BC1BEE22E409F96E93D7E117393172A
MAC: 070A16B46B4D4144F79BDD9DD04A287C
MSG: 6BC1BEE22E409F96E93D7E117393172AAE2D8A57
...
2
votes
0
answers
106
views
How to unpack const char* str into template<char...>?
Motivation:
Here is the implementation of converting base-n to decimal:
template<int from, char... Chs>
constexpr int
toDec()
{
int ret{};
return (
(...
7
votes
3
answers
3k
views
template parameter list for literal operator
Here is my implementation of converting binary literal to decimal:
template<char Head, char... Tail>
constexpr int operator"" _b()
{
if constexpr (sizeof... (Tail) == 0)
{
...
12
votes
1
answer
395
views
Inconsistency parsing numeric literals according to C++ Standard's grammar [duplicate]
Reading through the C++17 standard, it seems to me that there is an inconsistency between pp-number as handled by the preprocessor and numeric literals, e.g. user-defined-integer-literal, as they are ...
0
votes
0
answers
1k
views
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 ...
3
votes
2
answers
987
views
How to place user-defined literal inside constexpr class of same type in C++?
I want to implement my own string-literal class Literal and operator:
constexpr Literal operator""_s(const char* str, size_t size);
class Literal {
friend constexpr Literal operator"" _s(const ...
0
votes
1
answer
79
views
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(...
3
votes
2
answers
778
views
Invoke user-defined literal on lvalue
Is there any way to invoke a user defined literal on lvalues?
e.g I would like to
int operator "" _xor1(int a) { return a^1; }
// Works fine
17_xor1;
auto myint = ...
15
votes
3
answers
2k
views
How to use a user defined literal in a header file? [duplicate]
I have defined the following user-defined literal in MyLiteral.h:
namespace my_literals {
constexpr uint64_t operator"" _nanoseconds(unsigned long long int value) {
return value*1000;
...