user-defined-literals

When and how to use a template literal operator?

白昼怎懂夜的黑 提交于 2019-12-04 18:23:49
问题 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 non-type template parameter pack with element type char , such as template <char...> double operator "" _x(); So I wrote one like in the code below: template <char...> double operator "" _x() { return .42; } int main() { 10_x; // empty template list

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

笑着哭i 提交于 2019-12-04 03:19:57
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 write into a /var/log/myapp , what is the canonical way of accomplishing this in C/C++? Thoughts Do I have to do something crazy like setuid root if I don't want to sudo a_setup_script.sh ? Note that I am aware of the syslog routines, but they are insufficient for my needs (I need to log much more information, separated into different files, hence the

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

孤人 提交于 2019-12-04 00:13:00
This question already has an answer here: Compile error when using a member of a user-defined literal 1 answer 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 { return n; } }; constexpr foo operator ""_foo(unsigned long long test) { return foo{ static_cast<int>(test) }; } int main() { return 123_foo.bar(); } GCC and Clang reject it , saying they can't find an operator""_foo.bar . MSVC accepts it. If I

Using a C++ user-defined literal to initialise an array

有些话、适合烂在心里 提交于 2019-12-03 23:37:43
I have a bunch of test vectors, presented in the form of hexadecimal strings: MSG: 6BC1BEE22E409F96E93D7E117393172A MAC: 070A16B46B4D4144F79BDD9DD04A287C MSG: 6BC1BEE22E409F96E93D7E117393172AAE2D8A57 MAC: 7D85449EA6EA19C823A7BF78837DFADE etc. I need to get these into a C++ program somehow, without too much editing required. There are various options: Edit the test vectors by hand into the form 0x6B,0xC1,0xBE,... Edit the test vectors by hand into the form "6BC1BEE22E409F96E93D7E117393172A" and write a function to convert that into a byte array at run time. Write a program to parse the test

Integer sequence of chars from user-defined literal taking strings as parameters

柔情痞子 提交于 2019-12-03 14:02:34
Currently, only doubles can produce a template of chars in a user defined literal: template <char...> double operator "" _x(); // Later 1.3_x; // OK "1.3"_y; // C++14 does not allow a _y user- // defined operator to parse that as a template of chars Is there a clever way to produce a std::integer_sequence of chars using a user defined literal. In other words, what the code of _y(const char*, std::size_t) would be so that I end up with a std::integer_sequence<char, '1', '.', '3'> ? At this point in time, the best we can (portably) do is a macro trick as demonstrated for vtmpl::string .

Literal operator template: why not string?

让人想犯罪 __ 提交于 2019-12-03 13:23:44
Once again, while replying to another question, I forgot (my fault) that literal operator templates are picked up from the set of declarations only when integer or floating literals are found. As an example: template <char... C> constexpr int operator "" _x() { return 0; } It can be used as 10_x , but it cannot be used neither as foo_x nor as "foo"_x. Apart for the obvious reason that is because the standard says that , what's the technical reason (if any) for which they are not considered when dealing with string literals? I found also a proposal for that (well, not exactly the same, but it

When and how to use a template literal operator?

拜拜、爱过 提交于 2019-12-03 13:04:26
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 non-type template parameter pack with element type char , such as template <char...> double operator "" _x(); So I wrote one like in the code below: template <char...> double operator "" _x() { return .42; } int main() { 10_x; // empty template list, how to specify non-empty template parameters? } Question: The code works, but how can I use the

Are C++14 digit separators allowed in user defined literals?

南笙酒味 提交于 2019-12-03 10:21:06
While clang compiles the following line, g++ 6.1 complains about the digit separator (see live example on Coliru ): auto time = 01'23s; Which compiler, if any, is correct according to the C++14 standard (N3796)? Otherwise, is allowing digit separators (§2.14.2) just an implementation detail in the user-defined literals (§2.14.8) of the <chrono> library (§20.12.5.8)? IMHO it should be not, since these literals are defined on unsigned long long parameters. I remember Howard Hinnant using 10'000s as an example during his CppCon 2016 talk "A <chrono> tutorial" (at about 42 minutes in his talk).

Fixed-width integer literals in C++?

不羁的心 提交于 2019-12-03 05:58:25
C++11 first introduced support for defining new literals into C++ by means of user-defined literals . Does C++11 or later also predefine suffixes for fixed-width integer literals for types in <cstdint> ? No. As of C++14 the only literal suffixes defined by the standard are provided by <chrono> , <complex> and <string> headers in the standard library. The <chrono> header defines the h , min , s , ms , us , ns suffixes for time durations, <complex> defines the i , il and if suffixes for imaginary numbers, and <string> defines the s suffix for basic_string literals. However, one can easily define

How to refer to user defined literal operator inside a namespace?

大兔子大兔子 提交于 2019-12-01 16:24:34
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::test; /* doesn't work */ } I can refer to the user defined literal operator inside the namespace X by an explicit using namespace X; . Is there any way of referring to the literal operator without explicitly including the namespace? I tried the 10_X::test; but of course doesn't work as the parser believes X refers to the name of the operator. X::operator ""_test(10) works but it's clumsy. #include <iostream> namespace X {