Get decltype function

I want to get the type of a function and create std::vector . For example, I have

 int foo(int a[], int n) { return 1; } int bar(int a[], int n) { return 2; } 

and such a vector of such functions will be:

 std::vector< std::function<int(int[],int)> > v; 

And generally, a decltype() would be better, for example:

 std::vector< decltype(foo) > v; 

however, this will result in a compilation error.

I think the reason is that decltype() cannot distinguish

 int (*func)(int[], int) std::function<int(int[], int)> 

Is there any way to fix this?

+5
source share
3 answers

Use either:

 std::vector< decltype(&foo) > v; 

or

 std::vector< decltype(foo)* > v; 

or

 std::vector< std::function<decltype(foo)> > v; 

However, this will fail if foo is overloaded.

+12
source

To expand the answer Peter Skotnitsky

 decltype(foo) 

Has type

 int(int[], int) 

This is not a function pointer. To get a pointer to a function, you need to either use decltype with the address foo decltype(&foo) , or add the end * to the end of the type to declare a pointer to the type foo decltype(foo)*

+6
source

The solution is this:

 typedef std::function<int(int[], int)> sf; std::vector< sf > v2; 

and this is normal

0
source

Source: https://habr.com/ru/post/1232713/


All Articles