Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of -> in a template in order to force the following symbol to be dependent

Tags:

c++

templates

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.

like image 735
John Humphreys Avatar asked Dec 04 '22 06:12

John Humphreys


1 Answers

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.

like image 196
Pubby Avatar answered Dec 05 '22 19:12

Pubby