user-defined-literals Questions
1
I am learning about user defined literals and I wrote the following program that works with gcc and msvc but clang rejects it. Live demo
#include <array>
template<std::size_t N>
struct...
Goodson asked 18/2 at 13:15
21
Solved
Being able to create and manipulate strings during compile-time in C++ has several useful applications. Although it is possible to create compile-time strings in C++, the process is very cumbersome...
Johannessen asked 7/4, 2013 at 2:10
5
Solved
I have a user-defined literal operator that only makes sense for strings of a specific length, like this:
constexpr uint16_t operator "" _int(const char* s, std::size_t len)
{
return len == 2 ? s...
Klenk asked 29/11, 2017 at 1:22
2
Solved
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 ...
Classless asked 2/8, 2023 at 20:15
2
Solved
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...
Downtrend asked 25/3, 2023 at 0:52
5
Solved
The strings topic in the SO Documentation used to say, in the Remarks section:
Since C++14, instead of using "foo", it is recommended to use "foo"s, as s is a string literal, w...
Offshore asked 28/7, 2016 at 2:51
2
I want to use the std::chrono::duration literals, such as 10s to mean "10 seconds", like this:
std::chrono::duration<uint64_t, std::milli> millisecs = 10s;
But, I'm getting this er...
Gaul asked 24/5, 2018 at 7:26
3
Solved
I have a bunch of test vectors, presented in the form of hexadecimal strings:
MSG: 6BC1BEE22E409F96E93D7E117393172A
MAC: 070A16B46B4D4144F79BDD9DD04A287C
MSG: 6BC1BEE22E409F96E93D7E117393172AAE2D8...
Harless asked 24/12, 2018 at 12:59
2
Solved
#include <chrono>
namespace X
{
using namespace std;
struct A
{
std::chrono::seconds d = 0s; // ok
};
}
namespace Y
{
struct B
{
std::chrono::seconds d = 0s; // error
};
}
The error mes...
Signify asked 3/1, 2017 at 13:1
1
Solved
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
templ...
Rodolforodolph asked 3/6, 2020 at 18:3
1
Solved
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...
Dryly asked 28/5, 2020 at 9:30
2
Solved
void operator"" test( const char* str, size_t sz )
{
std::cout<<str<<" world";
}
int main()
{
"hello"test;
return 0;
}
In GCC 4.7, this generates "warning: literal operator suffix...
Doublequick asked 12/3, 2013 at 6:49
1
Solved
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&)...
Infare asked 20/1, 2019 at 15:54
3
Solved
Here is my implementation of converting binary literal to decimal:
template<char Head, char... Tail>
constexpr int operator"" _b()
{
if constexpr (sizeof... (Tail) == 0)
{
return Head - ...
Effusive asked 15/12, 2018 at 2:26
1
Solved
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 ...
Ule asked 11/12, 2018 at 11:39
3
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;
}
}...
Evaleen asked 20/6, 2018 at 7:47
1
Solved
At cppref, I find a strange C++ code, which uses a non-ascii character in source code as follows:
template <char...> double operator "" _π(); // OK
However, the code above cannot be cmompi...
Intrados asked 24/4, 2018 at 13:49
2
Solved
Generally, using namespace in global scope is considered as a bad practice. However, according to cppreference, the identifiers not starting with underscore(_) are reserved for standard library in ...
Erlond asked 27/3, 2018 at 1:42
1
Solved
Consider the following:
#include <iostream>
namespace X
{
void operator ""_test(unsigned long long x)
{
std::cout << x;
}
}
int main()
{
using namespace X;
10_test;
// 10_X::t...
Malva asked 1/3, 2018 at 16:35
1
Solved
Since C++11, it has been possible to create User Defined Literals. As expected, it's possible to return complex structs from such literals. However, when trying to use such operators as 123_f...
Dulcedulcea asked 1/3, 2018 at 7:23
1
Solved
I'm learning about user-defined literals, and confused with the following test code:
std::chrono::seconds operator"" _s(unsigned long long s) {
return std::chrono::seconds(s);
}
std::string oper...
Aspirator asked 24/11, 2017 at 6:10
1
Solved
For user defined string literals, is the given string guaranteed null terminated if I use the following form of definition? I know that the size given with second parameter count without any termin...
Unstudied asked 27/6, 2017 at 12:1
1
Solved
I know this is an old feature but reading on user defined literals e.g.
return_t operator "" _a(long); // Literal operator for user-defined INTEGRAL literal
I was reminded that
the leading u...
Whitehurst asked 29/1, 2017 at 0:2
5
Solved
I'm testing out user defined literals. I want to make _fac return the factorial of the number.
Having it call a constexpr function works, however it doesn't let me do it with templates as the comp...
Leenaleeper asked 12/11, 2011 at 23:52
1
Solved
Suppose I have some class:
template <typename T>
class Foo {
const T* x_;
public:
Foo(const T* str) : x_{str} {}
};
and I provide some user-defined literals that create a Foo object:
Fo...
Zimmermann asked 6/1, 2017 at 13:2
1 Next >
© 2022 - 2024 — McMap. All rights reserved.