Question

Consider the following function that takes a function as argument.

template <class Function = std::plus<int> > 
void apply(Function&& f = Function());

Here std::plus<int> is the default function that is applied. std::plus<int> is a function object and all works well.

Now, I would like to pass std::forward<int> as the default argument. std::forward<int> is not a function object, this is a function pointer. How to do that ?

template <class Function = /* SOMETHING */ > 
void apply(Function&& f = /* SOMETHING */);
Was it helpful?

Solution

The type of a function pointer to std::forward<int> is int &&(*)(int &). So your function would look like this:

template<class T = int &&(*)(int &)>
void apply(T &&t = &std::forward<int>);

Take a look at how std::forward is declared: http://en.cppreference.com/w/cpp/utility/forward

OTHER TIPS

I think this will work:

template <class Function = decltype(&std::forward<int>)> 
void apply(Function&& f = &std::forward<int>);

Edit: actually, maybe not. You're better off just overloading it:

void apply() {
  apply(&std::forward);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top