4

Consider the following function in C++11:

template<class Function, class... Args, typename ReturnType = /*SOMETHING*/> 
inline ReturnType apply(Function&& f, const Args&... args);

I want ReturnType to be equal to the result type of f(args...) What do I have to write instead of /*SOMETHING*/ ?

1
  • 4
    Wouldn't decltype(f(args...)) do it?
    – chris
    Jan 3, 2013 at 18:07

3 Answers 3

15

I think you should rewrite your function template using trailing-return-type as:

template<class Function, class... Args> 
inline auto apply(Function&& f, const Args&... args) -> decltype(f(args...))
{
    typedef decltype(f(args...)) ReturnType;

    //your code; you can use the above typedef.
}

Note that if you pass args as Args&&... instead of const Args&..., then it is better to use std::forward in f as:

decltype(f(std::forward<Args>(args)...))

When you use const Args&..., then std::forward doesn't make much sense (at least to me).

It is better to pass args as Args&&... called universal-reference and use std::forward with it.

2
  • Is it better to pass the arguments as universal references or as constant references ?
    – Vincent
    Jan 3, 2013 at 18:17
  • @Vincent: Universal references. That is better.
    – Nawaz
    Jan 3, 2013 at 18:17
4

It doesn't need to be a template parameter, since it isn't used for overload resolution. Try

template<class Function, class... Args> 
inline auto apply(Function&& f, const Args&... args) -> decltype(f(std::forward<const Args &>(args)...));
7
  • What is the difference between a version with and whithout std::forward ?
    – Vincent
    Jan 3, 2013 at 18:08
  • He is passing Args by const &. So does it much make sense to use std::forward?
    – Nawaz
    Jan 3, 2013 at 18:08
  • @Nawaz: Maybe not, but I suspect it should actually be Args&& ...args. However that's a different question. I just want this answer to be robust in the face of such changes.
    – Ben Voigt
    Jan 3, 2013 at 18:09
  • 1
    @Vincent: The difference is that this doesn't break if you decide to use perfect forwarding in the future.
    – Ben Voigt
    Jan 3, 2013 at 18:10
  • Does this even work? The way I see it std::forward<Args>(args) would cast args from const Args& to Args&& (note that I talk about single elements of the pack of ease of expression), meaning it would remove the constness (if it even works, which it shouldn't, since forward<T> should take either T& or T&&, but not const T&).
    – Grizzly
    Jan 4, 2013 at 8:45
0

There are situations where std::result_of is more useful. For example, say you want to pass this function:

int ff(int& out, int in);

and inside apply() call it like so

int res;
f(res, args...);

then I wouldn't know how to use decltype, because I have no int lvalue reference at hand. With result_of, you don't need variables:

template<class Function, class... Args> 
typename std::result_of<Function(const Args&...)>::type apply(Function&& f, const Args&... args)
{
  typedef typename std::result_of<F(const Args&...)>::type ReturnType;

  // your code
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.