Every now and then I see people writing C++ code containing heresy in the vein of the following:
This is no more legal C++ than
const int qux = 42;
int * quux = &qux;
It is not undefined, unspecified or implementation defined! It is simply illegal
The following is a legit, albeit a little obscure, C89 program. It is also a legit C++11 (and above) program.
auto a = 42;
Consider the following declaration
It’s artificial (generated with the ever-helpful geordi bot), although I’m sure that if you looked hard enough, a similar one would appear somewhere in the wild. In the above case, foo is a pointer to function taking pointer to function taking char* and char* and returning int returning pointer to array of char*. I think that even seasoned C and C++ programmers will agree that this is quite confusing at first glance. Or second. And third. Especially for people less versed in “C gibberish”, as cdecl.org aptly calls it.
Delegating constructors is one of the cool new features of C++11. I don’t want to repeat the information that’s already on countless other blogs and sites, so here’s a quick recap:
When writing template code, being able to detect whether a type has a method with a given signature may be very useful. For example, when writing a custom buffer, checking for T::size() may allow us to preallocate memory in an input function. Checking for T::operator bool() will tell us whether our type is testable without writing specializations for every such type, which would be especially tedious for unnamed types. (side note: lambdas don’t have operator bool(), but they’re implicitly convertible to function pointers when their capture list is empty, which should make them testable, but they aren’t in VC 10
Achieving the desired result requires basic understanding of SFINAE and overload resolution. Let’s say we want to check for existence of ::size() as used in the standard library or in the codebase of Qt, that is size_t (T::*)() const and int (T::*)() const respectively.