I am frequently finding myself dealing with templates and return values of functions in said templates. And I invariably end up with something like this:
template<typename Data>
auto foo(const Data& d) ->
typename std::decay<decltype(reinterpret_cast<const Data*>(0)->operator()(0, 0))>::type
{
typedef typename std::decay<decltype(reinterpret_cast<const Data*>(0)->operator()(0, 0))>::type return_t;
...
}
While this works, it is ugly and not really obvious what I want from a quick look. Is there a more readable, less 'hackish' way to get "the return type of calling this method on the template argument" ?
Without alias templates c++11:
template <typename Data>
auto foo(const Data& d) -> typename std::decay<decltype(d(0, 0))>::type
{
return {};
}
or:
template <typename Data
, typename return_r = typename std::decay<
typename std::result_of<const Data(int, int)>::type
>::type>
return_r foo(const Data& d)
{
return {};
}
With alias templates (c++14 or written by hand):
template <typename Data>
auto foo(const Data& d) -> std::decay_t<decltype(d(0, 0))>
{
return {};
}
or:
template <typename Data
, typename return_r = std::decay_t<std::result_of_t<const Data(int, int)>>>
return_r foo(const Data& d)
{
return {};
}
Use std::invoke_result_t
in c++17.