Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use result_of with const overloaded member function

Tags:

c++

typetraits

Is there a way to get result_of to work with const overloaded member functions? The regular way demonstrated in cppreference doesn't work since the address of the overloaded function can't be resolved.

#include <type_traits>

class C {
public:
   auto& f () { return x_; }
   const auto& f() const { return x_; }
private:
   int x_;
};

using FRet = std::result_of_t<decltype(&C::f)(C)>;

Wandbox

like image 662
Danra Avatar asked Dec 18 '22 01:12

Danra


1 Answers

Do not use result_of in this situation - you will have to manually disambiguate the overload. You can simply use decltype and std::declval:

using FRet = decltype(std::declval<C>().f());

If you want to hit the const overload, use:

using FRet = decltype(std::declval<const C>().f());
like image 73
Vittorio Romeo Avatar answered Dec 24 '22 00:12

Vittorio Romeo