3 ms·
C++14's "meow"s is syntactic sugar for string("meow"), with identical efficiency.
by StephanTLavavej 12y ago
C++14's "meow"s is syntactic sugar for string("meow"), with identical efficiency.
- mike-cardwell 12y agoIf that's the case, then when I do: auto str = "something"; Why is "str" a "const char " rather than a std::string? Tested using "-std=c++14" with both gcc version 4.9.1, clang 3.5.0 and with both libstdc++ and libc++ ? [edit] If you're the guy who does the MSDN videos on C++, thank you*. They've been incredibly useful to me.
- StephanTLavavej 12y agoSorry, wasn't checking my HN comments frequently (unlike Reddit there's no little red icon). "meow" is a traditional string literal, whose type is const char [5] (array of 5 const chars). When you say "auto", you get the same deduction as when you pass something to a template foo(T t) taking by value. This triggers "decay", where arrays decay to pointers. Hence auto (and T) is const char *. "meow"s is a user-defined literal (the Standard Library is a user as far as the compiler is concerned), for which you must include <string> and say "using namespace std::string_literals;" or something equivalent. The specification for UDLs says that this calls operator""s() which returns a std::string by value. Yep, I'm the video guy. Glad you like them!