I was reading one of the old Guru of the Week articles on typename
, #35. At the very end, you can find the following snippet:
#include <iostream> using std::cout; using std::endl; struct Rose {}; struct A { typedef Rose rose; }; template<class T> struct B : T { typedef typename T::rose foo; }; template<class T> void smell( T ) { cout << "awful" << endl; } void smell( Rose ) { cout << "sweet" << endl; } int main() { smell( A::rose() ); smell( B<A>::foo() ); }
I don't get this. My first guess was that the second smell
invocation led to the template smell
being instantiated due to something you easily overlook (what should the joke be about, otherwise?!). But both calls lead to "sweet" being printed out. And isn't that to be expected, after all? In typedef Rose rose;
, Rose
is not a dependent name, so that's fine. In typedef typename T::rose foo;
, rose
is dependent, but typename
mitigates that. My question(s):
Here is a condensed version of the snippet on godbolt. I tested every compiler that looked old (e.g. gcc-4.4.1
, but note that the above snippet is still 11 years older than gcc-4.4.1
).
Your understanding of the code is correct. The joke here is a reference to the line from Shakespeare's Romeo and Julliet:
What's in a name? That which we call a rose
By any other name would smell as sweet;
Which is often paraphrased as "A rose by any other name would smell as sweet."
Or in the case of this code:
A::Rose
, by any other type name, would still makesmell()
print"sweet"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With