I am pretty sure this is a beginner question. However, all my google searches failed me. So, here I am.
To begin, let's say, I have a few function like,
1 2 3 4 5 6 7 8
template<typename T>
T foo1(T& a){return a+1;}
template<typename T>
T foo2(T& a){return a-2;}
template<typename T>
T foo3(T& a){return a*3;}
Now, I want to overload the function call operator for vector. So, something similar to,
1 2 3 4 5
template<class T>
std::vector<T> operator()(std::vector<T>& vec)
{
for(auto& n:vec){//I don't know what to do here}
}
I can do it for a specific function. But what I want to do is that overload the operator in general such that if the argument is vector, it will call the (arbitrary) function for each element. So that I can call like this,
1 2 3 4
std::vector<int> a = {1,2,3,4};
std::vector<int> b = foo1(a);
std::vector<int> c = foo2(a);
std::vector<int> d = foo3(a);
Is this possible? How? what should be overload function's statement?
@George P, Sorry for the misunderstanding. I checked the constructor examples but none of them fits what I need. std::transform is not in that link. I meant that, what I want to do can be done using transform but I would prefer not for certain reason.
@George P, perhaps my explanation is bad. The functions themselves don't take vector. The functions only take one single argument and returns a single. So, the function call should be, double x = foo1(3.0);. I want to overload the function call operator such that when the function is called with a vector instead, the operator overload function will call the function foo1 for every element of the vector. Now, the function can be any arbitrary function. Not a specific function.
@JLBorges, I am trying to overload the function call operator. Not the functions. Overloading each function individually would defeat the purpose here.
You keep changing the requirements, as well as the examples of what you want. Your explanations are all over the place.
First you want functions that work and return a std::vector. Yet now you want functions that are passed a vector and return a double. M'ok, so what is supposed to happen to the vector's elements to return that singular double?
What EXACTLY are you trying to do? Explain THAT is more detail. LOTS more detail.
You're in better hands with JLBorges. I'm outta here.
Did you want fmap? This is a function that takes a function on elements and returns a new function that works with vectors of elements. Although there is somewhat more to it than this.