简体   繁体   中英

How can I get the function's type from a call expression?

I want to make unit test to check (and to learn how function resolving works), for function overloading that take lvalue instead of const lvalue reference for primitive types in some math functions, that the good function is being called. I did not found any usual pattern about it.

struct A {/* ... */};

A f(A) {} // #1
A&& f(A&&) {} // #2
// Or some very complicated template overloading

int main() {
    f(A()); // call #2 ; How to get it ?
}

// I want to have:
// typeid(f).name();

Is it possible to get typeid(f) from context at compile-time ? (like gcc s complaining)

"Pseudo code" :

std::cout << typeid(f with args A()).name() << std::endl 
// >> "A&& f(A&&)"   (#2)

With only typeid(f) , the compiler (gcc 7.3.0 on Ubuntu x64)

/home/xyzz/project/tests/RunTests.cpp:22: erreur : overloaded function with no contextual type information
     typeid(tst).name();
            ^~~

With typeid(f()) , it returns actually type 类型

Suggested answer: std::result_of . The template parameters are not rebuild, but we can get the function type.

The following seems to work on my machine. How close is it to what you want?

    std::cout
      << typeid( static_cast<void (*)(A)>(f) ).name() << std::endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM