Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want a vector of functions that would look something like this: vector<function<auto(auto)>>v; but obviously this didn't work...

something like this:
C++
#include<bits stdc++.h="">
using namespace std;
int f1(int n){
return n*2+2;
}

string f2(string n){
return n+'!';
}
int main(){
vector<function<auto(auto)>>v;v.clear();//not working

v.push_back(f1);v.push_back(f2);

cout<<v[0](5)<<endl<<v[1]("thanks");

return 0;
}


What I have tried:

if i do something like this for example:
vector<function<int(int)>>v1; and vector<function<string(string)>>v2;
and then push_back every function where it belongs everything is fine...
but i want one vector for all of them..if that is possible
Posted
Updated 5-Aug-19 5:14am
v3
Comments
Richard MacCutchan 4-Aug-19 5:06am    
Why, what problem are you trying to solve?
Member 14549814 4-Aug-19 10:10am    
no certain one..but it would help me in general
Richard MacCutchan 4-Aug-19 10:52am    
Help you to do what? The issue here, is that unless we know what you are trying to achieve then it is difficult to make suggestions.

Here's one way it can be done.
typedef UINT( WINAPI * ThreadFunc )( PVOID );

typedef struct
{
    ThreadFunc  function;
    PVOID       argument;
} OneFunction;

int main()
{
    std::vector< OneFunction > functions;

    OneFunction f;

    // set the members of f here

    functions.push_back( f );

    (* functions[0].function )( functions[0].argument );  // call one
}
You can define different function types also. ThreadFunc is just one type. If you have to, you can cast between different types as needed also so they don't all have to return a UINT. They can return any type you want and accept any arguments you want. Just define the various types so you can cast between them as necessary.
 
Share this answer
 
v2
Comments
Member 14549814 4-Aug-19 15:18pm    
Sorry but i'm getting several errors:
typedef 'ax' is initialized (use decltype instead)
'WINAPI' was not declared in this scope
'ThreadFunc' was not declared in this scope
'ThreadFunc' does not name a type
'PVOID' does not name a type|
Rick York 4-Aug-19 16:07pm    
Adjust it to use the types you want/have/need. PVOID is a pointer to void.
Member 14549814 4-Aug-19 16:58pm    
i'm not used to that kind of stuff...however..thanks anyway
Rick York 9-Aug-19 13:57pm    
If you are not used to this kind of stuff then you should NOT even think about going down this path because that's what is required.
Stefan_Lang 5-Aug-19 10:17am    
Don't you see? Once you need to cast a function to the appropriate type, the entire concept of storing these function pointers into an array falls apart: casting requires knowing the correct function - and then you can just as well invoke the function normally! Even if a cast weren't necessary, you'd need to know the argument types to correctly invoke the function, and the return type to correctly receive and interpret the result - all you'd hide is the name of the function and any documentation that comes with it.

Of course, the entire question is likewise doomed to be left unanswered: there is no way to solve this in such a way that the vector of functions would actually serve a purpose other than needlessly obfuscating the code.
Don't do this. It's probably doable with variadic templates, but judging by your code you already have enough trouble without templates.

Moreover, putting functions (or rather references to functions) into a container eliminates readability, makes your code hard to read and prone to errors.

Additionally, since you cannot invoke these functions without knowing what function it is (in order to supply the correct parameters), there is no gain in doing this at all! Maybe if you could specify what kind of benefit you thought you would gain, we could point you to a different, more sensible approach.
 
Share this answer
 
Comments
Rick York 5-Aug-19 17:21pm    
I agree with you fully. After all the casts and such it will just be a mess.
Member 14549814 5-Aug-19 17:25pm    
i do not have a specifically use of this thing..but i found it interesting .. and i became courious to find out how you would write this thing
Stefan_Lang 6-Aug-19 3:22am    
Technically, the virtual function table of virtual classes does something very similar: effectively it's a list of function pointers containing a pointer for each virtual member function. It's based on the class definition however, which does provide the full list of function signatures at compile time. Moreover, the VFT is only used by derived classes and invoked by their original name and signature. The code doesn't look like accessing a vector of function pointers at all - and that is a good thing!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900