Using & # 8594; in the template to make the next character be dependent

From the question:

Proper use of this->

The answer states that β†’ can be used

... in the pattern, to make the next character be dependent - in this last use, this is often inevitable.

What does this mean, what would be a good example of this use? I don’t quite understand what β€œdepends” in this context, but it sounds like a useful trick.

+4
source share
3 answers

Posted in another 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 does not know that x is an (inherited) element.

Similar to using typename and template inside the 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 }; 

This is stupid and somewhat unnecessary, but you should still do it.

+12
source
 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 at C ++ Frequently Asked Questions.

+5
source

See Using this keyword in a destructor [closed] .

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

0
source

Source: https://habr.com/ru/post/1383461/


All Articles