Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hidden Features of C++? [closed]

Most C++ programmers are familiar with the ternary operator:

x = (y < 0) ? 10 : 20;

However, they don't realize that it can be used as an lvalue:

(a == 0 ? a : b) = 1;

which is shorthand for

if (a == 0)
    a = 1;
else
    b = 1;

Use with caution :-)


You can put URIs into C++ source without error. For example:

void foo() {
    http://stackoverflow.com/
    int bar = 4;

    ...
}

Pointer arithmetics.

C++ programmers prefer to avoid pointers because of the bugs that can be introduced.

The coolest C++ I've ever seen though? Analog literals.


I agree with most posts there: C++ is a multi-paradigm language, so the "hidden" features you'll find (other than "undefined behaviours" that you should avoid at all cost) are clever uses of facilities.

Most of those facilities are not build-in features of the language, but library-based ones.

The most important is the RAII, often ignored for years by C++ developers coming from the C world. Operator overloading is often a misunderstood feature that enable both array-like behaviour (subscript operator), pointer like operations (smart pointers) and build-in-like operations (multiplying matrices.

The use of exception is often difficult, but with some work, can produce really robust code through exception safety specifications (including code that won't fail, or that will have a commit-like features that is that will succeed, or revert back to its original state).

The most famous of "hidden" feature of C++ is template metaprogramming, as it enables you to have your program partially (or totally) executed at compile-time instead of runtime. This is difficult, though, and you must have a solid grasp on templates before trying it.

Other make uses of the multiple paradigm to produce "ways of programming" outside of C++'s ancestor, that is, C.

By using functors, you can simulate functions, with the additional type-safety and being stateful. Using the command pattern, you can delay code execution. Most other design patterns can be easily and efficiently implemented in C++ to produce alternative coding styles not supposed to be inside the list of "official C++ paradigms".

By using templates, you can produce code that will work on most types, including not the one you thought at first. You can increase type safety,too (like an automated typesafe malloc/realloc/free). C++ object features are really powerful (and thus, dangerous if used carelessly), but even the dynamic polymorphism have its static version in C++: the CRTP.

I have found that most "Effective C++"-type books from Scott Meyers or "Exceptional C++"-type books from Herb Sutter to be both easy to read, and quite treasures of info on known and less known features of C++.

Among my preferred is one that should make the hair of any Java programmer rise from horror: In C++, the most object-oriented way to add a feature to an object is through a non-member non-friend function, instead of a member-function (i.e. class method), because:

  • In C++, a class' interface is both its member-functions and the non-member functions in the same namespace

  • non-friend non-member functions have no privileged access to the class internal. As such, using a member function over a non-member non-friend one will weaken the class' encapsulation.

This never fails to surprise even experienced developers.

(Source: Among others, Herb Sutter's online Guru of the Week #84: http://www.gotw.ca/gotw/084.htm )


One language feature that I consider to be somewhat hidden, because I had never heard about it throughout my entire time in school, is the namespace alias. It wasn't brought to my attention until I ran into examples of it in the boost documentation. Of course, now that I know about it you can find it in any standard C++ reference.

namespace fs = boost::filesystem;

fs::path myPath( strPath, fs::native );

Not only can variables be declared in the init part of a for loop, but also classes and functions.

for(struct { int a; float b; } loop = { 1, 2 }; ...; ...) {
    ...
}

That allows for multiple variables of differing types.