4

From the question:

Proper use of this->

The answer states that -> can be used

...in a template, in order to force the following symbol to be dependent—in this latter use, it is often unavoidable.

What does this mean what what would a good example of this use be? I don't quite what "dependent" means in this context, but it sounds like a useful trick.

Community
  • 1
  • 1
John Humphreys
  • 37,047
  • 37
  • 155
  • 255

3 Answers3

12

Posted in other question:

template <class T>
struct foo : T {
  void bar() {
    x = 5;       // doesn't work
    this->x = 5; // works - T has a member named x
  }
};

Without this-> compiler doesn't know x is a (inherited) member.

Similar to use of typename and template inside template code:

template <class T, class S>
struct foo : T {
  typedef T::ttype<S>; // doesn't work
  typedef typename T::template ttype<S> footype; // works
};

It's silly and somewhat unnecessary, but you still gotta do it.

Pubby
  • 51,882
  • 13
  • 139
  • 180
  • Okay, thanks for the answer :) now to stop myself from posting another question on this one... typename T::template ttype - why do you need ::template on this line? – John Humphreys Nov 28 '11 at 17:43
  • @w00te There is an [excellent FAQ entry](http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) that explains when and where to use `typename` and `template` – Praetorian Nov 28 '11 at 17:45
  • Thanks Praetorian - I'll check that out :) – John Humphreys Nov 28 '11 at 17:45
  • 3
    @w00te It's using `template` as a qualifier. Without it the compiler would see the ` – Pubby Nov 28 '11 at 17:46
  • 1
    "_It's silly and somewhat unnecessary, but you still gotta do it._" It's absurd that `typedef` does not imply `typename`: there is no fundamental reason why `typename` is necessary just after `typedef`. OTOH, the fundamental need for `typename`, `template` and `this->` actually derive from the modern name binding rules of templates (see my answer). – curiousguy Nov 29 '11 at 05:24
  • @curious i showed in another question that c++ syntax is too flexible for that. for ex, `T typedef foo;` is perfectly valid. but there is no typename after `typedef` – Johannes Schaub - litb Nov 29 '11 at 08:17
  • @JohannesSchaub-litb What?!! This grammar is really awful. – curiousguy Nov 29 '11 at 08:26
5
template <typename T>
struct Base
{
  void foo() {}
};

template <typename T>
struct Derived : Base<T>
{
  void bar()
  {
    // foo();  //foo() is a dependent name, should not call it like this
    // Base<T>::foo(); //This is valid, but prevents dynamic dispatch if foo is virtual
    this->foo(); //use of this-> forces foo to be evaluated as a dependent name
  }
};

A more detailed explanation is available on the C++ FAQ

Praetorian
  • 106,671
  • 19
  • 240
  • 328
0

See Using this keyword in destructor [closed].

This is ugly, but this ugliness derives directly from the general name binding rules of "modern" templates (as opposed to macro-like template, as implemented by MS).

Community
  • 1
  • 1
curiousguy
  • 8,038
  • 2
  • 40
  • 58