C++在成员函数范围中使用语句

HC4*_*ica 6 c++ templates using-declaration

如果我想从模板派生类中使用模板基类的成员,我必须将其带入范围:

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

template <typename T>
struct derived : base<T>
{
    using base<T>::foo;
};
Run Code Online (Sandbox Code Playgroud)

为什么我不能将此using语句放入本地范围,就像其他using语句一样?

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
    }
};
Run Code Online (Sandbox Code Playgroud)

Ben*_*igt 1

标准(草案 3225)中说道[namespace.udecl]

\n\n
\n

类成员的using 声明应是member-declaration。[ 例子:

\n
\n\n
struct  X  {\n    int  i;\n    static  int  s;\n};\nvoid  f()  {\n    using  X::i; // error:  X::i is a class member\n                 // and this is not a member declaration.\n    using  X::s; // error:  X::s is a class member\n                 // and this is not a member declaration.\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

\xe2\x80\x94 结束示例]

\n
\n\n

using指令没有这样的限制,但是 ( [namespace.udir]):

\n\n
\n

在using-directive中查找命名空间名称时,仅考虑命名空间名称

\n
\n