Reputation: 6646
I've been having this question for some time. Suppose we have a contrived function:
template<typename F>
std::result_of_t<std::decay_t<F>(???)> transform(F&& f)
{
static const int num = 42;
return std::forward<F>(f)(num);
}
The thing I'm not sure of is whether I should use int
or const int&
for the ???
part. Similarly, for this function:
template<typename F>
std::result_of_t<std::decay_t<F>(???)> transform(F&& f)
{
ExpensiveType foo;
return std::forward<F>(f)(std::move(foo));
}
Should I use ExpensiveType
or ExpensiveType&&
for the ???
part?
Upvotes: 0
Views: 153
Reputation: 40895
Use auto!
C++14:
template < typename F >
auto transform(F&& f)
{
constexpr auto num = 42;
return std::forward<F>(f)(num);
}
C++11:
template < typename F >
auto transform(F&& f) -> decltype(std::forward<F>(f)(42))
{
// ... same body
}
Upvotes: 2