首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在成员函数尾部返回类型中使用这个和属性?

在成员函数尾部返回类型中使用这个和属性?
EN

Stack Overflow用户
提问于 2012-01-26 13:03:27
回答 3查看 2.2K关注 0票数 6

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

但是,clang3.0(见下文)和gcc 4.5.2都没有接受它。

代码语言:javascript
复制
#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";
}

clang 3.0说:

代码语言:javascript
复制
$ 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的支持最多是很麻烦的,我在标准(n3290)中找不到具体的限制。

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

所以,这是允许的还是不允许的?

奖金:更多最新版本的clang / gcc支持这一点吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-01-26 13:20:54

我记错了。这是某一点上的缺陷,但最终是已解决并投票加入国防情报局

§5.1.1 [expr.prim.general]

如果声明声明了类X的成员函数或成员函数模板,则表达式this是在可选的cv-qualifer和函数定义、成员-声明器或声明器结束之间的“指向cv-限定符-seqX”类型的prvalue。

正因为如此,Clang和GCC还没有正确地实施它。

代码语言:javascript
复制
struct X{
  // 'this' exists between the | markers
  void f() const volatile | {
  } |
  auto g() const volatile | -> void {
  } |
};
票数 9
EN

Stack Overflow用户

发布于 2012-01-26 21:51:03

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

代码语言:javascript
复制
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);
  }
};

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

票数 5
EN

Stack Overflow用户

发布于 2012-01-26 13:22:14

我不知道你写的东西是否合法,但还有其他一些方法可以达到你想要的目的:

代码语言:javascript
复制
  template <typename F>
  auto apply(F& f) -> decltype(f(*(MyClass*)0, (int)0)) {
    return f(*this, _arg);
  }

或者:

代码语言:javascript
复制
  template <typename F>
  typename std::result_of<F(MyClass,int)>::type apply(F& f) {
    return f(*this, _arg);
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9018460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档