Created
April 21, 2015 17:33
-
-
Save sjolsen/6c6338b08e902ca2430a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <type_traits> | |
template <typename Param, typename Arg> | |
Param take_address_if_necessary_impl (Arg&& arg, std::true_type, std::false_type) | |
{ | |
return arg; | |
} | |
template <typename Param, typename Arg> | |
Param take_address_if_necessary_impl (Arg&& arg, std::false_type, std::true_type) | |
{ | |
return &arg; | |
} | |
template <typename Param, typename Arg> | |
Param take_address_if_necessary (Arg&& arg) | |
{ | |
return take_address_if_necessary_impl <Param> ( | |
arg, | |
typename std::is_convertible <Arg, Param>::type {}, | |
typename std::is_convertible <typename std::add_pointer <Arg>::type, Param>::type {} | |
); | |
} | |
template <typename Ret, typename... Params, typename... Args> | |
Ret call_special (Ret (*f) (Params...), Args&&... args) | |
{ | |
return f (take_address_if_necessary <Params, Args> (args)...); | |
} | |
template <typename... Params, typename... Args> | |
void call_special (void (*f) (Params...), Args&&... args) | |
{ | |
f (take_address_if_necessary <Params> (args)...); | |
} | |
#include <iostream> | |
void function (int* i, char* c) | |
{ | |
std::cout << *i << ' ' << *c << std::endl; | |
} | |
int main () | |
{ | |
int i = 42; | |
char c = '%'; | |
call_special (function, 1, 'f'); | |
call_special (function, &i, '?'); | |
call_special (function, &i, &c); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment