Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When can `typename` not be replaced by `class`? [duplicate]

Tags:

c++

templates

Possible Duplicate:
C++ difference of keywords 'typename' and 'class' in templates

I already know in many cases that class cannot be replaced by typename. I am only talking about the opposite: replacing typename by class.

Someone pointed out that only typename can be used here:

template<class param_t> class Foo 
{     
        typedef typename param_t::baz sub_t; 
};

But I do not see any problem replacing typename with class here (in MSVC). To recap, can I ALWAYS replace typename with class? Please give an example if not.

like image 274
Rio Wing Avatar asked Oct 30 '25 14:10

Rio Wing


1 Answers

No, you cannot always replace one with the other.

The two keywords typename and template are necessary for name disambiguation to inform the compiler whether a dependent name is a value (no keyword needed), a type (needs typename), or a template (needs template):

template <typename T> struct Foo
{
  char bar()
  {
    int x = T::zing;                 // value, no decoration for disambiguation of "T::zing"

    typedef typename T::bongo Type;  // typename, require disambiguation of "T::bongo"

    return T::template zip<Type>(x); // template, require disambiguation of "T::zip"
  }
};

Only the keywords typename and template work in those roles; you cannot replace either by anything else.

like image 71
Kerrek SB Avatar answered Nov 02 '25 05:11

Kerrek SB