6

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

但是,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";
}

铿锵 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.

嗯……不太好。

但是,在大多数编译器中,对 C++11 的支持充其量只是 hacky,我找不到标准 (n3290) 中提到的具体限制。

在评论中,Xeo 暗示这可能是标准中的一个缺陷......

那么,这是否允许?

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

4

3 回答 3

9

我记错了。这在某些时候是一个缺陷,但最终被解决并投票加入了 FDIS

§5.1.1 [expr.prim.general]

如果声明声明了类的成员函数或成员函数模板X,则表达式this是可选的cv -qualifer-seq函数定义的结尾、成员-声明者,或声明者X

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

struct X{
  // 'this' exists between the | markers
  void f() const volatile | {
  } |
  auto g() const volatile | -> void {
  } |
};
于 2012-01-26T13:20:54.197 回答
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);
  }
};

取模,是的 usingthis在 C++11 中以尾随返回类型有效。

于 2012-01-26T21:51:03.443 回答
1

我不知道你写的是否合法,但还有其他一些方法可以实现你想要的:

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

或者:

  template <typename F>
  typename std::result_of<F(MyClass,int)>::type apply(F& f) {
    return f(*this, _arg);
  }
于 2012-01-26T13:22:14.310 回答