c++ - 缺陷报告 1207

标签 c++ c++11 language-lawyer

我不明白 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_iteratorconst 重载>。 另一方面,尾随返回类型中的 v.begin()“调用”返回 iterator 的非常量重载。

问题是类型不一致。

关于c++ - 缺陷报告 1207,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25754016/

相关文章:

c++ - c++14 中的静态 thread_local 内存异步信号安全吗?

c++ - 如何在 Microsoft Visual Studio C/C++ 编译器中关闭位置无关代码编译?

c++ - 迭代器在与临时容器交换后变得无效

c++ - char* 和 std::uint8_t* 之间的 reinterpret_cast - 安全吗?

c++ - std::forward_list -- 用存储的迭代器删除

c++ - 模糊指向堆或堆栈对象的智能指针

c - 检索传递给可变参数函数的 int32_t 的可移植方法

c++ - 这会限制该类仅在当前帧中具有生命周期吗?

c++ - 诸如 bits/vector.tcc 之类的头文件名是否符合标准?

C++ 复合字面量