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
    }
};
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
  • 1
    What problem are you trying to solve by doing this? Are you trying to avoid prefixing the name `foo` with `this->`? – James McNellis Feb 24 '11 at 04: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. – HighCommander4 Feb 24 '11 at 04:59

2 Answers2

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

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    I *can* call `foo()` without any qualification if I put `using base::foo` at class scope in the derived class. – HighCommander4 Feb 24 '11 at 05:20
  • @HighCommander4: `using base::foo` is not needed even at class scope if you want to call `foo` from `f()`. – Nawaz Feb 24 '11 at 05:23
  • Whether using `this->` is simpler than using `using base::foo` is a matter of preference. I only need to write `using base::foo` once, whereas I would need to write `this->` in front of every call to `foo()`. – HighCommander4 Feb 24 '11 at 05:25
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

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • I see. Any ideas about the rationale behind this? – HighCommander4 Feb 24 '11 at 05:33
  • 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. – Ben Voigt Feb 24 '11 at 06:16