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?
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.