我不明白 this defect report 1207 的原因,更具体地引用以下句子(强调是我的):
Because the transformation of a member name into a class member access expression (9.3.1 [class.mfct.non-static] paragraph 3) only occurs inside the body of a non-static member function, the type of v in the trailing-return-type is non-const but is const in the return expression, resulting in a type mismatch between the return expression and the return type of the function.
编辑
也就是说,我不明白为什么尾随返回类型中 v 的类型被推导为非常量。
最佳答案
vector v;
auto end() const -> decltype(v.begin()) { return v.begin(); }
decltype(v.begin())
,在 trailing-return-type 中,是 iterator
- 因为 v
的类型是 vector
,从外面看。
但是在函数体内,考虑了成员函数end()
的常量说明符。
v
的类型是 this->v
的类型 - 这又取决于 this
的常量性。
this
' pointee 是 const
(因为前面提到的 const-specifier),因此成员函数内的 this
的类型是 block const*
。
因此成员函数中this->v
的类型是vector const
(因为const访问路径),v.begin( )
- 实际上是 (this->v).begin()
- 调用返回类型为 const_iterator
的 const
重载>。
另一方面,尾随返回类型中的 v.begin()
“调用”返回 iterator
的非常量重载。
问题是类型不一致。
关于c++ - 缺陷报告 1207,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25754016/