سؤال

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 */);
هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top