functions with different names but same arguments

Navid Mahmoudian

I have a set of functions with the same number of arguments, but different names (suppose each implement different method). I have the name of the requested method in a string. Is it possible to call the corresponding function using the method name. to make it clear, suppose that I have the following function (I just mentioned their name and argument):

search_esa(int a, int b);
search_tss(int a, int b);
search_fss(int a, int b);
search_ds(int a, int b);

now lets assume I have the name of the requested method, lets assume esa, in a string:

string methodName = "esa"

is it possible to call the corresponding function by using macros? I tested

#define CALL_SEARCH_MV(method) search_##method

and then try to call it by:

CALL_SEARCH_MV(methodName.c_str())

but it calls search_methodName function, which does not exist. Any other suggestions would be greatly appreciated (except suggesting switch case method or if).

ZeroUltimax

You can use a combination of Macros, function pointers and clever hacking to achieve the desired result with ease.

In your main.cpp (or where the search functionality lives):

#include <map>
#include <string>
#include <iostream>

typedef int(*search)(int a, int b);

#define SEARCH(x) int search_##x(int a, int b);
#include "search_func.incl"
#undef SEARCH

#define SEARCH(x) {#x, search_##x},
std::map<std::string, search> search_funcs = {
#include "search_func.incl"    
};
#undef SEARCH


int main(char** argv, int argc)
{
    std::cout << search_funcs["esa"](1, 2) << std::endl;
    std::cout << search_funcs["tss"](3, 4) << std::endl;
    std::cout << search_funcs["fss"](5, 6) << std::endl;
    std::cout << search_funcs["ds"](7, 8) << std::endl;

    return 0;
}

int search_esa(int a, int b) { return 100 * a + 10 * b + 0; }
int search_tss(int a, int b) { return 100 * a + 10 * b + 1; }
int search_fss(int a, int b) { return 100 * a + 10 * b + 2; }
int search_ds(int a, int b) { return 100 * a + 10 * b + 3; }

In search_func.incl

SEARCH(esa)
SEARCH(tss)
SEARCH(fss)
SEARCH(ds)

How this works The search_func.incl file specifies the name of your search functions with a macro. When you include the file, you switch up the definition of the macro so that the included functions are presented differently.

SEARCH(abc) 

becomes a function declaration on the first include:

int search_abc(int a, int b);

and a key value pair {string, function pointer} on the second include:

{"abc", search_abc},

These KVP are in a map, so when you need to call a function, the string maps to the function name automatically.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

c variadic functions, same arguments, different formats

From Dev

How to not repeat myself in this situation? C functions that are the same but have different arguments

From Dev

Callback functions with different arguments

From Dev

Different arguments with same template

From Dev

How can I combine 2 or more jquery functions with elements that do the same thing but with different names

From Java

Different arguments, but behavior of function is same

From Dev

Use same arguments with different command

From Dev

Different structs with the same variable names

From Dev

Different names for different objects of same type

From Dev

Same functions in different views - Django

From Dev

Functions with different signatures, but the same body

From Dev

Functions with same name, different origin

From Dev

Using the same parameter in different functions

From Dev

sequenced before:the order of arguments of different functions

From Dev

Python passing different arguments to a list of functions

From Java

Jest mock the same function twice with different arguments

From Dev

Function returns different answers with same arguments

From Dev

Template class methods with different arguments but same code

From Dev

Ruby threads calling the same function with different arguments

From Dev

how to conditionally run different command with same arguments

From Dev

Python - overload same function with different arguments

From Dev

How to parallelize the same function using different arguments?

From Dev

Multiple applications with different names for the same code base

From Dev

Multiple applications with different names for the same code base

From Dev

Same type names different namespaces with Ravendb

From Dev

Execute same block of code with different variable names

From Dev

Is same Tag Names for Different Depths in XML valid?

From Dev

Values are same but names are different using linq

From Dev

Read different xml Nodes with same names

Related Related

  1. 1

    c variadic functions, same arguments, different formats

  2. 2

    How to not repeat myself in this situation? C functions that are the same but have different arguments

  3. 3

    Callback functions with different arguments

  4. 4

    Different arguments with same template

  5. 5

    How can I combine 2 or more jquery functions with elements that do the same thing but with different names

  6. 6

    Different arguments, but behavior of function is same

  7. 7

    Use same arguments with different command

  8. 8

    Different structs with the same variable names

  9. 9

    Different names for different objects of same type

  10. 10

    Same functions in different views - Django

  11. 11

    Functions with different signatures, but the same body

  12. 12

    Functions with same name, different origin

  13. 13

    Using the same parameter in different functions

  14. 14

    sequenced before:the order of arguments of different functions

  15. 15

    Python passing different arguments to a list of functions

  16. 16

    Jest mock the same function twice with different arguments

  17. 17

    Function returns different answers with same arguments

  18. 18

    Template class methods with different arguments but same code

  19. 19

    Ruby threads calling the same function with different arguments

  20. 20

    how to conditionally run different command with same arguments

  21. 21

    Python - overload same function with different arguments

  22. 22

    How to parallelize the same function using different arguments?

  23. 23

    Multiple applications with different names for the same code base

  24. 24

    Multiple applications with different names for the same code base

  25. 25

    Same type names different namespaces with Ravendb

  26. 26

    Execute same block of code with different variable names

  27. 27

    Is same Tag Names for Different Depths in XML valid?

  28. 28

    Values are same but names are different using linq

  29. 29

    Read different xml Nodes with same names

HotTag

Archive