c++ - Why can't decltype work with overloaded functions? -
decltype
fails if function you're calling on overloaded, in code:
#include <iostream> int test(double x, double y); double test(int x, int y); char test(char x, int y); int main() { std::cout << decltype(test) << std::endl; return 0; }
results:
error: decltype cannot resolve address of overloaded function
i understand because decltype
can't figure out function you're trying type of. why isn't there way make work, this:
std::cout << decltype(test(double, double)) << std::endl;
or this:
double x = 5, y = 2; std::cout << decltype(test(x, y)) << std::endl;
since function cannot overloaded based on return type, wouldn't passing either datatypes or actual variables decltype
call enough tell of overloads it's supposed examine? missing here?
to figure out type of function type of arguments you'd pass, can "build" return type using decltype
, "calling" types, , add on parameter list piece entire type together.
template<typename... ts> using testtype = decltype(test(std::declval<ts>()...))(ts...);
doing testtype<double, double>
result in type int(double, double)
. can find full example here.
alternatively, might find trailing return type syntax more readable:
template<typename... ts> using testtype = auto(ts...) -> decltype(test(std::declval<ts>()...));
Comments
Post a Comment