user-defined-literals

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

自作多情 提交于 2020-01-25 06:52:29
问题 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 */ STRINGIFY_AS_UDL(foo) // -> "foo"_udl STRINGIFY_AS_UDL(bar) // -> "bar"_udl STRINGIFY_AS_UDL(42) // -> "42"_udl 回答1: UDL operators are also "regular" functions, so you can call them instead: #define STRINGIFY_AS_UDL(id) operator ""_udl(#id) but you can

Using user-defined literals in expressions sometimes requires whitespace

▼魔方 西西 提交于 2020-01-21 11:07:27
问题 The following code compiles in both GCC and Clang: long double operator""_a(long double); auto x = 0e1_a+0; // OK But not this (replacing _a with _e ): long double operator""_e(long double); auto y = 0e1_e+0; // Error: unable to find numeric literal operator 'operator""_e+0' OTOH, this code compiles: auto z = 0e1_e +0; // OK What's going on? (This question is inspired by this GCC bug report.) 回答1: Maximal munch strikes again. [lex.pptoken]/p3: If the input stream has been parsed into

Using user-defined literals in expressions sometimes requires whitespace

懵懂的女人 提交于 2020-01-21 11:07:26
问题 The following code compiles in both GCC and Clang: long double operator""_a(long double); auto x = 0e1_a+0; // OK But not this (replacing _a with _e ): long double operator""_e(long double); auto y = 0e1_e+0; // Error: unable to find numeric literal operator 'operator""_e+0' OTOH, this code compiles: auto z = 0e1_e +0; // OK What's going on? (This question is inspired by this GCC bug report.) 回答1: Maximal munch strikes again. [lex.pptoken]/p3: If the input stream has been parsed into

Which user-defined-literals are predefined by the standard?

a 夏天 提交于 2020-01-13 08:26:42
问题 My question sounds like a contradiction, but I don't know how else to refer to the new literal syntax other than user-defined-literal . std::string operator "" s ( const char* str, size_t len ) { return std::string( str, len ); } assert( "foo"s == "bar"s ); I remember hearing that user defined literals should start with an _ prefix. That would imply that the library defines some non-prefixed literals for us. Does the standard provide some UDLs in the the standard library? If yes, what are

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

梦想的初衷 提交于 2020-01-12 07:59:06
问题 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'> ? 回答1: At

Is it possible to disable GCC warning about missing underscore in user defined literal?

大城市里の小女人 提交于 2020-01-03 07:25:21
问题 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 suffixes not preceded by '_' are reserved for future standardization [enabled by default]" I understand why this warning is generated, but GCC says "enabled by default". Is it possible to disable this warning without just disabling all warnings via the -w flag? 回答1: After reading several comments to this question, I