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

auto foo(...) ->decltype(this) Is there some workaround?

本文关键字:解决 方法 decltype foo gt auto this      更新时间:2023-10-16

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

template<class Key, int b> class b_plus_tree_inner_node {
  auto split() -> decltype(this) {}
};

给我这样的错误

在顶级中无效使用"this"

我可以用另一种方式来做吗?我现在担心typedef的存在,但decltype可能吗?

编辑:

我想完成这个:

b_plus_tree_inner_node<Key, b>* split() {...}

如果您想要成员函数,请在类中声明它:

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(){}
};

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

template<class Key, int b>
b_plus_tree_inner_node<Key, b>* split(){}

该标准确实允许您编写auto split() -> decltype(this) {},但GCC 4.6还不支持它(GCC 4.7的主干支持它)。

您可能想要这个:

template<class Key, int b> 
class b_plus_tree_inner_node 
{        
     b_plus_tree_inner_node<Key, b> split() 
     { 
          return /*...*/;
     }
};