Search code examples
c++templatesusing-declaration

C++ using statement in member function scope


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
    }
};

Solution

  • 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