user-defined-literals

Why aren't C++14 standard-defined literals in the global namespace by default?

限于喜欢 提交于 2019-12-18 14:38:31
问题 C++14 includes standard-defined literals for, amongst other things, std::string and various timespans from the <chrono> header. To use them you must say using namespace std::literals; (or some variation depending on exactly which literals you want, as they're in a variety of inline namespaces). All this is good, but I'm curious as to why the using declaration is required. UDLs without a leading underscore are reserved for the implementation, so there is no possibility that "hello world"s

User defined literal arguments are not constexpr?

假装没事ソ 提交于 2019-12-18 14:06:05
问题 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 compiler complains that the arguments are not and cannot be constexpr . I'm confused by this - aren't literals constant expressions? The 5 in 5_fac is always a literal that can be evaluated during compile time, so why can't I use it as such? First method: constexpr int factorial_function(int x) { return

Using macro with string fails on VC 2015

大城市里の小女人 提交于 2019-12-17 19:37:38
问题 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__ should have been processed by the pre-processor. What is going on here? I thought of some mix-match issue with Unicode/non-Unicode build - but it doesn't help. It's not just issue with pre-defined macros, but with user defined also: #define MACRO "abc"

What new capabilities do user-defined literals add to C++?

时光总嘲笑我的痴心妄想 提交于 2019-12-17 02:25:36
问题 C++11 introduces user-defined literals which will allow the introduction of new literal syntax based on existing literals ( int , hex , string , float ) so that any type will be able to have a literal presentation. Examples: // imaginary numbers std::complex<long double> operator "" _i(long double d) // cooked form { return std::complex<long double>(0, d); } auto val = 3.14_i; // val = complex<long double>(0, 3.14) // binary values int operator "" _B(const char*); // raw form int answer =

Conveniently Declaring Compile-Time Strings in C++

ⅰ亾dé卋堺 提交于 2019-12-16 22:22:11
问题 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, as the string needs to be declared as a variadic sequence of characters, e.g. using str = sequence<'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!'>; Operations such as string concatenation, substring extraction, and many others, can easily be implemented as operations on sequences

Can I invoke at run-time the logic for choosing which user-defined literal to call?

醉酒当歌 提交于 2019-12-13 03:34:38
问题 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(unsigned long long int count) { return {count / 60.f, true}; } constexpr Duration operator""_s(unsigned long long int count) { return {count / 3600.f, true}; } Duration holds a number of hours (as a float) and a validity flag. So I can say: Duration duration = 17_m; And I can say: int m = 17;

How to automatically add literal definitions, based on a single user-defined literal?

ぃ、小莉子 提交于 2019-12-10 02:23:50
问题 C++11 offers user-defined literals. I've just started to play around with them, which made me wonder whether it would be possible to automatically add all SI multipliers to a single literal I define? For example, if I define Length operator "" _m(long double m) { return Length(m); // Length in meters } where Length is a subclass of some Units base class, I would like to have a mechanism to automatically add (in the same spirit as boost operators) SI multipliers for all literals that return a

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

梦想的初衷 提交于 2019-12-09 07:23:28
问题 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

Should a using command issue a warning when using a reserved identifier?

纵然是瞬间 提交于 2019-12-08 15:46:29
问题 When using the line using std::literals::chrono_literals::operator""s; in g++ 6.3.0, the compiler issues a warning stating: warning: literal operator suffixes not preceded by '_' are reserved for future standardization using std::literals::chrono_literals::operator""s; A similar warning is also issued in MSVS. However, clang 3.8.0 issues no such warning. Since operator""s is defined by the standard for the chrono library shouldn't this not issue a warning since we are just importing the name

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

左心房为你撑大大i 提交于 2019-12-05 15:57:27
问题 This question already has an answer here : Compile error when using a member of a user-defined literal (1 answer) Closed last year . 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() {