Even though I'm aware C++ can be used to achieve better performance, this comment doesn't imply it. The fact it allows more places to tune for performance, if it were poor enough would not mean it gets better than other language.C++ has the power of optimizations because it allows more places to tune for performance and do it without a heavy burden over all codebase. Like you can override new operator.
Another thing you omitted about it, is that it's also more space friendly than including C code in headers, since, except if you BOTH declared it as static (that's probably needed to avoid linker errors anyway) and the compiler removes unused code, C functions will get compiled into the modules including them, while templates are build on demand (i.e., you include the template, but only the used specializations get compiled).Templates are powerful for performance (at least when we talk code without LTO) as the templates have to be parts of headers, so all compilers do have access both to type specialization (like using sort function) and to all code to make assumptions. STL (at least with unchecked iterators) have good performance algorithms which are a bit out of reach of the typical programmer: I talk here for example that the default std::map implementation is based on red-black trees which are balanced binary trees. Yes, you can write it in C, but is not made in a transparent way for users.
I see one: you need to transform it to the IR. This is harder as the language gets more complex, and at the very least this means a longer compile time. Anyway, this difference was negligible in 2006 (in the article I told about, I promise I'll look for it tomorrow), so right now it should be the same or maybe a bit better for C++ (since I believe it received the most efforts since then). Also, C++11 introduced several features aiming to better performance and to ease the work for the compiler.At last, C++ is harder to successfully write a compiler, but most compilers target the same intermediate reprezentation as their C counterpart (like LLVM, GCC's GIMPLE, or whatever way they are abstarcting the code does), so I see no reason that a C++ code would perform (if you switch the extension from C to C++ or vice-versa) worst or better.
I don't think what the average programmer would write is that relevant, since there are libraries for C that someone caring about such things as performance could use, which probably have good algorithms. I wouldn't judge a language based on how crappy (and NIH-ish) some programmers could be. Of course, it's nice to have the templates to avoid looking for a good library for fairly common things.Even I prefer Generic collections (from BCL/.Net world), std::vector gives basically the same performance of iterating with an indexer as a C array goes (again with unchecked iterators) and many STL algorithms are better than average programmer would write.
Thanks, I'll look into it.A great C++ talk about using some tricks (there is one very close to C world, and one where std::move semantic is flawed in today's compiler implementations), and one of them is using templates to optimize bit operations:
(sorry that is a MS site channel, but the talk is about GCC/Facebook/C++/templates)