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
15
votes
3 answers

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 MAC: 7D85449EA6EA19C823A7BF78837DFADE etc. I…
TonyK
  • 16,761
  • 4
  • 37
  • 72
15
votes
3 answers

How to use a user defined literal in a header file?

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; } } Now I could use the operator in another…
Alexander
  • 1,068
  • 6
  • 22
15
votes
3 answers

When and how to use a template literal operator?

On cppreference there is a mentioning that one can have templated user-literal operators, with some restrictions: If the literal operator is a template, it must have an empty parameter list and can have only one template parameter, which must be a…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
15
votes
1 answer

Using macro with string fails on VC 2015

Why does this fail to compile? char programDate[] = "("__DATE__")"; But this compiles fine (see space): char programDate[] = "(" __DATE__")"; I do know VC2015 now supports literal-operators. But shouldn't that be in compilation phase? __DATE__…
Ajay
  • 18,086
  • 12
  • 59
  • 105
15
votes
1 answer

String literal with dependent type — impossible?

Is it possible to define a user-defined string literal conversion operator such that the type of its result depends on the value of its string input? It is easy with user-defined integer and floating point literals because they admit literal…
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
15
votes
2 answers

Code I've never seen in C++11

I'm looking at this source code template struct conv2bin; template struct conv2bin { static_assert(high == '0' || high == '1', "no bin num!"); static int const value = (high - '0')…
13
votes
1 answer

Can user defined numeric literals be immediately followed by a dot?

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_foo.bar(): struct foo { int n; int bar() const {…
Justin
  • 24,288
  • 12
  • 92
  • 142
13
votes
2 answers

How to make [std::operator""s] visible in a namespace?

#include 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 message is: error : no matching literal operator for…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
13
votes
1 answer

Why don't complex-number literals work in clang?

When I run this code on ideone.com, it prints (2,3): #include #include int main() { std::complex val = 2 + 3i; std::cout << val << std::endl; return 0; } But when I use clang on macOS 10.11.6, I get no…
jtbandes
  • 115,675
  • 35
  • 233
  • 266
13
votes
1 answer

g++ 4.7 evaluates operator "" as sibling to macro expansion

I'm moving some code over to GCC 4.7 (from 4.6) and ran into a few compiler errors and found the problem documented in the GCC 4.7 porting guide: User-defined literals and whitespace The C++ compiler in ISO C11 mode…
Travis Gockel
  • 26,877
  • 14
  • 89
  • 116
12
votes
2 answers

Are user-defined-literals resolved at compile-time or runtime?

I wonder, because predefined literals like ULL, f, etc. are obviously resolved at compile time. The standard (2.14.8 [lex.ext]) doesn't seem to define this, but it seems to tend towards runtime: [2.14.8 / 2] A user-defined-literal is treated as a…
Xeo
  • 129,499
  • 52
  • 291
  • 397
12
votes
1 answer

Inconsistency parsing numeric literals according to C++ Standard's grammar

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 defined to be handled by the "upper"…
12
votes
1 answer

Compile error when using a member of a user-defined literal

When compiling this code (without any header) template struct Temperature { T temp; explicit Temperature(T t) : temp(t) {} }; Temperature operator "" _f (long double t) { return Temperature
neuront
  • 9,312
  • 5
  • 42
  • 71
12
votes
1 answer

typeid(complex(0.0,1.0)) != typeid(1.0i)

Using gcc 4.9 I found that types generated with type literal for complex numbers are not the same as when created by conventional means, i.e.: typeid(complex(0.0,1.0)) != typeid(1.0i) Am I making a mistake here? Is this a compiler bug or…
roland
  • 123
  • 6
12
votes
1 answer

How to write an C/C++ application that writes to a /var/log/myapp directory?

Background On Linux systems, Application Logs exist in subdirectories of /var/log, which is owned by root/root and has 755 permissions on my system. For example, I see /var/log/mysql and /var/log/samba. Question If I want a myapp to be able to…
kfmfe04
  • 14,936
  • 14
  • 74
  • 140
1
2
3
10 11