Hello, everyone. It has been a while since my last post.
While watching CppCon 2014 I found this little gem being described by Scott Meyers.
Consider the following code:
static int Foo;
static int Bar;
decltype(auto) a = foo();
decltype(auto) b = bar();
cout << foo() << ", " << bar() << endl;
Would you expect this to print 0, 0? Well, not so fast. decltype(auto) is just a nicer syntax for decltype(name), where name is “unparenthesized id-expression or an unparenthesized class member access”, as described here. Therefore it follows the same rules.
That’s another corner case to remember, but I guess it could also make a nifty party trick, if one can find the right party.
According to grammar rules defined by the C++ standard (and C as well) the following code is valid:
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.