I want to make a template function that takes a function and a vector and uses the function to map that vector to another vector that will be returned by the function template.
If the function taken as an argument is a free function, it may have one of two signatures.
// T is the parameter of the function template
T sig1(const T x);
T sig2(const T x, const std::vector<T>& v);
It may also be a function object in which operator()
would behave like the free functions. Use of the function template for any of the 4 possibilities should be transparent.
std::vector<int> v;
// ... fill v somehow ...
// foo is either free function or function object instance
const std::vector<int> a = map_vec(foo, v);
I asked how to do this for C++11 and got a great answer from 0x499602D2.
"Overload" function template based on function object operator() signature
0x499602D2's answer makes use of the fact that these are two distinct template signatures in C++11:
template<typename F, typename T>
auto map_vec(F&& fnc, const std::vector<T>& source)
-> decltype(void(fnc(std::declval<T>())), std::vector<T>{});
template<typename F, typename T>
auto map_vec(F&& fnc, const std::vector<T>& source)
-> decltype(void(fnc(std::declval<T>(), source)), std::vector<T>{});
I would also like to know how to solve this in C++98.
Here is my effort so far. I have a SFINAE struct that can determine if a function objects takes two args. I don't know how to get this working for both function objects and free functions. Either I need to change the SFINAE struct to work on both function objects and free functions or I need to use overloading to route function objects and free functions separately.
http://coliru.stacked-crooked.com/a/1471088cbc3b8544
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…