If I want to use a member of a template base class from a template derived class, I have to bring it into scope as such:
template <typename T>
struct base
{
void foo();
};
template <typename T>
struct derived : base<T>
{
using base<T>::foo;
};
Why can't I place this using statement into a local scope, like other using 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
}
};
The purpose of using base<T>::foo
in the function scope is that you want to call foo
in the function, and since it gives error, you cannot do that.
If you want to call the functon (otherwise why you would do that), then you can do these which are 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!
Demo at ideone : http://www.ideone.com/vfDNs
Read this to know when you must use template
keyword in a function call:
Ugly compiler errors with template
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With