为什么decltype看不到成员声明?

编程入门 行业动态 更新时间:2024-10-26 10:35:09
本文介绍了为什么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看不到成员声明?

本文发布于:2023-10-16 05:19:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496615.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声明   成员   decltype

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!