auto foo(...) - > decltype(this)有一些解决方法吗?

Yol*_*ola 3 c++ c++11

我有下一个类,并尝试声明成员函数,它将返回指向该类型但下一个代码的指针

template<class Key, int b> class b_plus_tree_inner_node {
  auto split() -> decltype(this) {}
};
Run Code Online (Sandbox Code Playgroud)

给了我这样的错误

在顶层无效使用'this'

我可以用另一种方式做到这一点,我现在关于typedef的存在,但是它可能与decltype有关吗?

编辑:

我想完成这个:

b_plus_tree_inner_node<Key, b>* split() {...}
Run Code Online (Sandbox Code Playgroud)

R. *_*des 5

如果你想要一个成员函数在类中声明它:

template<class Key, int b> class b_plus_tree_inner_node {
   b_plus_tree_inner_node* split(){}
   // also valid:
   //b_plus_tree_inner_node<Key, b>* split(){}
};
Run Code Online (Sandbox Code Playgroud)

如果您想要非成员函数,请将其设为模板:

template<class Key, int b>
b_plus_tree_inner_node<Key, b>* split(){}
Run Code Online (Sandbox Code Playgroud)

标准允许你写,auto split() -> decltype(this) {}但GCC 4.6还不支持它(GCC 4.7的主干).

  • "你只能在成员函数体上使用它." - 这不是真的."this"也可以在"decltype"中的尾随返回类型中使用.这在马德里是一成不变的,所以他的海湾合作委员会不支持它. (3认同)