我有一些课C
与 const
和非const
一些通用类型的 setter/getter Node
:
template <typename NodeType>
class CParent{};
class Node {};
class C : public CParent<Node> {
Node& getNode(Index i);
const Node& getNode(Index i) const;
};
现在我想创建一个调用 getNode
的别名函数对于 C
类的对象:
template <class CType>
NodeType& AliasGetNode(CType* cobject);
但是我如何推断 NodeType
?即,如果我调用 AliasGetNode<const C>(c)
和 AliasGetNode<C>(c)
, NodeType
应分别为const Node&
和 Node&
.
我该怎么做?
我试过 result_of
和 decltype
方法但没有成功。
最佳答案
让编译器推导出返回类型(从 C++14 开始):
template <class CType>
decltype(auto) AliasGetNode(CType& cobject)
{
return cobject.getNode(0);
}
关于c++ - 函数模板返回类型推导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41426753/