C++成员函数作用域中的using语句

发布于 2024-10-19 03:05:41 字数 530 浏览 1 评论 0原文

如果我想使用模板派生类中模板基类的成员,我必须将其带入作用域,如下所示:

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

template <typename T>
struct derived : base<T>
{
    using base<T>::foo;
};

为什么我不能将此 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
    }
};

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌︼了一个春 2024-10-26 03:05:41

在函数作用域中 using base::foo 的目的是你想在函数中调用 foo ,并且由于它给出错误,所以你不能这样做。

如果你想调用该函数(否则你为什么要这样做),那么你可以执行这些允许的操作:

this->template base<T>::foo(); //syntax 1
this->base<T>::foo();          //syntax 2 - simple
this->foo();                   //syntax 3 - simpler

但是,你不能这样写:

foo() ;  //error - since foo is in base class template!
//if you write `using base<T>::foo` at class scope, it will work!

ideone 上的演示: http://www.ideone.com/vfDNs

阅读本文以了解何时必须使用 函数调用中的 template 关键字:

模板的丑陋编译器错误

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

Smile简单爱 2024-10-26 03:05:41

标准(草案 3225)在 [namespace.udecl] 中表示:

类成员的使用声明应是成员声明。 [示例:

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

—结束示例]

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文