As I remember, primarily Linus was complaining, that C++ allows people to do things, which they don't grasp the computational cost of. That seems to put "1:Never" in doubt... |
That's true of C as well... and really any library for that matter.
If you don't know what a library is doing under the hood, and you use it improperly, then yes it's possible to produce less than efficient code.
On the other hand, the standard C++ libraries are well defined and if you understand them then it's not difficult to write simple code that's just as efficient as (and sometimes possibly even more efficient than) the C counterpart.
One particular example that I like is printf vs. cout.
1 2 3
|
printf("%d",myint); // C style approach
// vs.
cout << myint; // C++ style approach
| |
The C++ style approach is safer
AND more efficient than the C style approach.
It's safer because:
- printf doesn't check to see if 'myint' actually is passed or not. It's perfectly legal syntax to call
printf("%d");
, but the result is a potential program explosion.
- printf has no type information about 'myint'. If you pass a double instead of an int, it will compile without error, but will not output what you expect.
It's worth nothing that some compilers have improved to detect both of the above scenarios and will issue a warning if you do either one of them. So in that sense, compilers have made printf a lot safer than it used to be.
It's more efficient because:
- printf has to parse the passed string and find all of the '%' format tags
- printf has to determine which type 'myint' is supposed to be at runtime
- cout has no string parsing, and the type is determined by the compiler at compile time