为什么decltype看不到成员声明? [英] Why does decltype not see the member declaration?

查看:87
本文介绍了为什么decltype看不到成员声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试编译该简单类:

#include <vector>
struct M
{
// interface
    auto begin() -> decltype(identities.begin())
    {
        return identities.begin();
    }
// implementation
private:
    std::vector<int> identities;
};

导致错误:

$ g++-510 where.cpp -std=c++11
where.cpp:57:35: error: ‘struct M’ has no member named ‘identities’
     auto begin() ->decltype(this->identities.begin())
                                   ^
where.cpp:57:35: error: ‘struct M’ has no member named ‘identities’

$ clang++ where.cpp -std=c++11 -Wall -pedantic -Wextra
where.cpp:57:35: error: no member named 'identities' in 'M'
    auto begin() ->decltype(this->identities.begin())
                            ~~~~  ^

为什么不 decltype 看到类成员?

推荐答案

来自N3337 [basic.lookup.unqual] / 7:

From N3337 [basic.lookup.unqual]/7:


在X类外部定义中使用的名称成员函数体或嵌套类定义的声明必须以下列方式之一声明:

A name used in the definition of a class X outside of a member function body or nested class definition shall be declared in one of the following ways:


  • ss X或是X的基类的成员,或者...

因为尾随返回类型为是 declaration 函数的一部分,而不是 definition 的一部分,它无法向前查看该类中还声明了什么,因此您需要在该函数上方声明该成员声明。

Because the trailing return type is part of the function declaration rather than the definition, it can't look ahead to see what else is declared in the class, so you need to declare that member above the function declaration.

这篇关于为什么decltype看不到成员声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆