I have some class C with const and non-const getters for some generic type Node:
template <typename NodeType>
class CParent{};
class Node {};
class C : public CParent<Node> {
Node& getNode(Index i);
const Node& getNode(Index i) const;
};
Now I want to create an alias function that call getNode for an object of class C:
template <class CType>
NodeType& AliasGetNode(CType* cobject);
But how do I deduce NodeType? i.e., if I call AliasGetNode<const C>(c) and AliasGetNode<C>(c), NodeTypeshould be respectively const Node&and Node&.
How can I do this?
I tried the result_of and decltype approaches but have not been successful.
Let the compiler deduce the return type (as of C++14):
template <class CType>
decltype(auto) AliasGetNode(CType& cobject)
{
return cobject.getNode(0);
}
I would recommend the:
template <class CType>
auto AliasGetNode(CType& cobject) -> decltype(cobject.getNode(0))
{
return cobject.getNode(0);
}
This should fairly work since c++11
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With