Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Herb Sutter's code joke in GotW #35 on typename out of date?

Tags:

c++

typename

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):

  1. What's the point of this snippet? Am I missing a sense of humour here?
  2. The article is from 1998; were there any language changes that alter what this code does?

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).

like image 936
lubgr Avatar asked Jun 27 '19 13:06

lubgr


1 Answers

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 make smell() print "sweet".

like image 117
interjay Avatar answered Sep 18 '22 07:09

interjay