6

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
    }
};
2
  • 1
    What problem are you trying to solve by doing this? Are you trying to avoid prefixing the name foo with this->? Commented Feb 24, 2011 at 4:51
  • 2
    By using the using-declaration, I am avoiding prefixing the name foo with this->, yes. By placing it in a local scope, I am trying to pollute the derived scope only where necessary. Commented Feb 24, 2011 at 4:59

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

I can call foo() without any qualification if I put using base<T>::foo at class scope in the derived class.
@HighCommander4: using base<T>::foo is not needed even at class scope if you want to call foo from f().
Whether using this-> is simpler than using using base<T>::foo is a matter of preference. I only need to write using base<T>::foo once, whereas I would need to write this-> in front of every call to foo().
1

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

A using-declaration for a class member shall be 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 example ]

A using-directive has no such restriction, however ([namespace.udir]):

when looking up a namespace-name in a using-directive, only namespace names are considered

2 Comments

I see. Any ideas about the rationale behind this?
I can only think of one thing -- that would let you have member and non-member functions in the same name search space, which maybe can't happen under the current rules (does having a member function by a particular name hide namespace-scoped functions by the same name?). But I guess ADL would already do that, so I really have no idea why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.