Function arguments

How to write a function that will accept an argument of any c++ type but not any user defined type. Even if we succeed in doing so, how to know what type has been passed. Also if I want only specific type example like only int, string and string array. With one argument is this possible?
Also if I want only specific type example like only int, string and string array.

Ordinarily, you would use function overloads for this:
1
2
3
int f(int) { std::cout << "f(int) was called\n"; }
int f(std::string) { std::cout << "f(std::string) was called\n"; }
int f(std::string*) { std::cout << "f(std::string*) was called\n"; }


If there are many unrelated alternatives, or you need to accept only types with certain properties, you can do this too, but this sort of technique is a last-resort thing, to be avoided if possible:
http://coliru.stacked-crooked.com/a/53f686d67bd2d6de
Last edited on
Topic archived. No new replies allowed.