user-defined-literals

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

风流意气都作罢 提交于 2019-11-28 21:18:36
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() << std::endl; // does **NOT** work! return 0; } The compiler gives the following error message: error:

Using macro with string fails on VC 2015

ⅰ亾dé卋堺 提交于 2019-11-28 10:54:10
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" char data[] = "("MACRO")"; EDIT: Error C3688 invalid literal suffix '__DATE__'; literal operator or

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

…衆ロ難τιáo~ 提交于 2019-11-28 01:51:19
When compiling this code (without any header) template <typename T> struct Temperature { T temp; explicit Temperature(T t) : temp(t) {} }; Temperature<long double> operator "" _f (long double t) { return Temperature<long double>((t - 32) / 1.8); } int main() { auto t = 100.0_f; t.temp; 100.0_f.temp; // ERROR AT THIS LINE return 0; } The compilers (both g++ 4.8 and clang++ 3.4 on Ubuntu 14.04) will complain that error: unable to find numeric literal operator ‘operator"" _f.temp’ 100.0_f.temp; ^ It seems that the _f.temp is considered as a suffix there. Why do the compilers parse it like that,

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

我的梦境 提交于 2019-11-27 04:49:42
问题 When compiling this code (without any header) template <typename T> struct Temperature { T temp; explicit Temperature(T t) : temp(t) {} }; Temperature<long double> operator "" _f (long double t) { return Temperature<long double>((t - 32) / 1.8); } int main() { auto t = 100.0_f; t.temp; 100.0_f.temp; // ERROR AT THIS LINE return 0; } The compilers (both g++ 4.8 and clang++ 3.4 on Ubuntu 14.04) will complain that error: unable to find numeric literal operator ‘operator"" _f.temp’ 100.0_f.temp;