C ++ using an operator in the area of โ€‹โ€‹member functions

If I want to use a member of the base class of the template from the derived class of the template, I must bring it to the area as such:

template <typename T> struct base { void foo(); }; template <typename T> struct derived : base<T> { using base<T>::foo; }; 

Why can't I put this using statement in a local scope, for example, using other statements?

 template <typename T> struct base { void foo(); }; template <typename T> struct derived : base<T> { void f() { using base<T>::foo; // ERROR: base<T> is not a namespace } }; 
+6
c ++ templates using-declaration
source share
2 answers

The standard (draft 3225) says in [namespace.udecl] :

The use-declaration for a class member is a member declaration. [Example:

 struct X { int i; static int s; }; void f() { using X::i; // error: X::i is a class member // and this is not a member declaration. using X::s; // error: X::s is a class member // and this is not a member declaration. } 

- end of example]

There is no such restriction when using the pointer directive, however ( [namespace.udir] ):

when searching for a namespace name in the using directive, name names are counted

+1
source share

The purpose of using base<T>::foo in the function area is that you want to call foo in the function, and since it gives an error, you cannot do this.

If you want to call functon (otherwise you would do it), you can do this, which is allowed:

 this->template base<T>::foo(); //syntax 1 this->base<T>::foo(); //syntax 2 - simple this->foo(); //syntax 3 - simpler 

However, you cannot write this:

 foo() ; //error - since foo is in base class template! //if you write `using base<T>::foo` at class scope, it will work! 

Ideon Demo: http://www.ideone.com/vfDNs

Read this to know when you should use the template keyword in a function call:

Horrible template compiler errors

+2
source share

All Articles