A major problem with C++ is a lack of consistency, which you run into if you start trying to generalize the ideas presented in this article. The article demonstrates string concatenation using +. That is great! Except it's inconsistent. The article's example works fine, of course: return name+'@'+domain; I'll skip over the weird use of '' instead of "". Now let's say I want to prepend mailto: as well: return "mailto:…
Not that this is necessarily any cleaner, but in C++14 one would write: return "mailto"s + ":"s + name + "@"s + domain; and it would compile fine. (s being a literal for std::string)
Popular Myths about C++, Part 1
141–144 of 144 posts
Re: Popular Myths about C++, Part 1
#142Earlier quoted context omitted.
> return "mailto" + ":" + name+'@'+domain; Apparently, the first `+` is a problem, so just take it away: return "mailto" ":" + name+'@'+domain; // there, problem solved.
Of course. Now, do you want to sit down and explain why that's necessary to somebody who's still grappling with the concept of for loops?
Re: Popular Myths about C++, Part 1
#143Good luck trying to understand C++ without C. The numerous C++ traps and pitfalls will just look mad to you. BTW, 'multi-paradigm' is an oxymoron. Not even Scala uses that concept any more.
Scott Meyers says in Effective C++, view C++ as a confederation of languages: C, C++, the preprocessor and templates. When some people say that when you are learning C++ you should not be exposed to C, I really have to disagree.
Re: Popular Myths about C++, Part 1
#144Earlier quoted context omitted.
C++14's "meow"s is syntactic sugar for string("meow"), with identical efficiency.
If 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.
"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 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!