user-defined-literals

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

走远了吗. 提交于 2019-12-01 15:12:47
问题 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

g++ 4.7 evaluates operator “” as sibling to macro expansion

久未见 提交于 2019-12-01 02:48:53
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 std={c++11,c++0x,gnu++11,gnu++0x} supports user defined literals, which are incompatible with some valid ISO C++03 code. In particular, whitespace is now needed after a string literal and before something that could be a valid user defined literal. Take the valid ISO C++03 code const char *p = "foobar"__TIME__; In C++03, the TIME macro expands to some string literal and is

g++ 4.7 evaluates operator “” as sibling to macro expansion

痞子三分冷 提交于 2019-11-30 23:32:18
问题 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 std={c++11,c++0x,gnu++11,gnu++0x} supports user defined literals, which are incompatible with some valid ISO C++03 code. In particular, whitespace is now needed after a string literal and before something that could be a valid user defined literal. Take the valid ISO C++03 code

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

久未见 提交于 2019-11-30 11:04:13
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 could ever mean anything else in a standard-conforming programme. So why isn't #include <string> sufficient

User defined literal arguments are not constexpr?

我怕爱的太早我们不能终老 提交于 2019-11-30 10:56:47
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 (x > 0) ? x * factorial_function(x - 1) : 1; } constexpr int operator "" _fac(unsigned long long x) {

Advantages of using user-defined literal for strings instead of string literal

我是研究僧i 提交于 2019-11-30 07:48:19
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, which converts the const char * "foo" to std::string "foo" . The only advantage I see using std::string str = "foo"s; instead of std::string str = "foo"; is that in the first case the compiler can perform copy-elision (I think), which would be faster than the constructor call in the second case. Nonetheless, this is (not yet) guaranteed, so the first one might also call a constructor, the copy constructor. Ignoring

typeid(complex<double>(0.0,1.0)) != typeid(1.0i)

浪尽此生 提交于 2019-11-30 07:27:24
问题 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<double>(0.0,1.0)) != typeid(1.0i) Am I making a mistake here? Is this a compiler bug or intended standard behavior? If intended standard behavior: What is the rationale behind? Adding the missing MCVE #include <complex> using std::complex; using namespace std::literals::complex_literals; #include <iostream> using std::cout; using std:

Why do user-defined string literals and integer literals have different behavior?

时光总嘲笑我的痴心妄想 提交于 2019-11-30 06:46:42
问题 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 operator"" _str(const char *s, std::size_t len) { return std::string(s, len); } int main() { auto str = "xxxxx"_str; std::cout << str.size() << std::endl; // works auto sec = 4_s; std::cout << sec.count() << std::endl; // works std::cout << "xxxxx"_str.size() << std::endl; // works std::cout << 4_s.count()

Advantages of using user-defined literal for strings instead of string literal

孤街醉人 提交于 2019-11-29 10:34:28
问题 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, which converts the const char * "foo" to std::string "foo" . The only advantage I see using std::string str = "foo"s; instead of std::string str = "foo"; is that in the first case the compiler can perform copy-elision (I think), which would be faster than the constructor call in the second case. Nonetheless, this is

typeid(complex<double>(0.0,1.0)) != typeid(1.0i)

 ̄綄美尐妖づ 提交于 2019-11-29 03:47:22
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<double>(0.0,1.0)) != typeid(1.0i) Am I making a mistake here? Is this a compiler bug or intended standard behavior? If intended standard behavior: What is the rationale behind? Adding the missing MCVE #include <complex> using std::complex; using namespace std::literals::complex_literals; #include <iostream> using std::cout; using std::endl; #include <typeinfo> int main(int argc, char* argv[]) { if (typeid(complex<double>(0.0, 1.0)) ==