在成员函数尾随返回类型中使用this和attributes?

Mat*_* M. 6 c++ c++11 trailing-return-type

在我给出的答案中,使用尾随返回类型this中的类的属性_arg作为decltype表达式的一部分是有意义的.这可能没有,但不方便.

尽管如此,clang 3.0(见下文)和gcc 4.5.2都没有接受.

#include <iostream>

class MyClass {
public:
  MyClass(int i): _arg(i) {}

  template <typename F>
  auto apply(F& f) -> decltype(f(_arg)) {
    return f(_arg);
  }

  template <typename F>
  auto apply(F& f) -> decltype(f(*this, _arg)) {
    return f(*this, _arg);
  }

private:
  int _arg;
};

struct Id {
  template <typename V>
  V operator()(V v) const { return v; }
};

struct ComplexId {
  template <typename C, typename V>
  V operator()(C const&, V v) { return v + 1; }
};

int main() {
  Id id; ComplexId complex;

  MyClass c(0);

  std::cout << c.apply(id) << " " << c.apply(complex) << "\n";
}
Run Code Online (Sandbox Code Playgroud)

clang 3.0说:

$ clang++ -std=c++11 -Weverything test.cpp
test.cpp:8:38: error: use of undeclared identifier '_arg'
      auto apply(F& f) -> decltype(f(_arg)) {
                                     ^
test.cpp:8:45: error: type name requires a specifier or qualifier
      auto apply(F& f) -> decltype(f(_arg)) {
                                            ^
test.cpp:8:45: error: C++ requires a type specifier for all declarations
      auto apply(F& f) -> decltype(f(_arg)) {
                          ~~~~~~~~          ^
test.cpp:8:7: error: 'auto' return without trailing return type
      auto apply(F& f) -> decltype(f(_arg)) {
      ^
test.cpp:13:39: error: invalid use of 'this' outside of a nonstatic member function
      auto apply(F& f) -> decltype(f(*this, _arg)) {
                                      ^
test.cpp:13:52: error: type name requires a specifier or qualifier
      auto apply(F& f) -> decltype(f(*this, _arg)) {
                                                   ^
test.cpp:13:52: error: C++ requires a type specifier for all declarations
      auto apply(F& f) -> decltype(f(*this, _arg)) {
                          ~~~~~~~~                 ^
test.cpp:13:7: error: 'auto' return without trailing return type
      auto apply(F& f) -> decltype(f(*this, _arg)) {
      ^
8 errors generated.
Run Code Online (Sandbox Code Playgroud)

哼......不太好.

但是,在大多数编译器中,C++ 11的支持最多是hacky,我找不到标准中提到的特定限制(n3290).

在评论中,Xeo建议它可能是标准中的缺陷......

那么,这是允许还是不允许?

奖励:并且更新版本的clang/gcc支持这个吗?

Xeo*_*Xeo 9

我记错了.这在某种程度上一个缺陷,但最终得到了解决并投入了FDIS.

§5.1.1 [expr.prim.general]

如果声明声明了类的成员函数或成员函数模板X,则表达式this是可选的cv-qualifer-seq函数定义的结尾之间的"指向cv-qualifier-seq的 指针"的prvalue ,成员 -声明者声明者.X

因此,Clang和GCC还没有正确实现它.

struct X{
  // 'this' exists between the | markers
  void f() const volatile | {
  } |
  auto g() const volatile | -> void {
  } |
};
Run Code Online (Sandbox Code Playgroud)


Joh*_*itb 5

您的代码无效的C++ 11,因为在成员函数的返回类型中不认为该类是完整的.您只能访问之前已声明的成员.像这样

class MyClass {
private:
  int _arg;

public:
  MyClass(int i): _arg(i) {}

  template <typename F>
  auto apply(F& f) -> decltype(f(_arg)) {
    return f(_arg);
  }

  template <typename F>
  auto apply(F& f) -> decltype(f(*this, _arg)) {
    return f(*this, _arg);
  }
};
Run Code Online (Sandbox Code Playgroud)

模数,是使用this在尾随返回类型的C++ 11中有效.