Extract the return type of a function without calling it (using templates?)

后端 未结 7 1700
温柔的废话
温柔的废话 2020-12-16 21:49

I\'m looking for a way in C++ to extract the return type of a function (without calling it). I presume this will require some template magic.

float Foo();
i         


        
7条回答
  •  死守一世寂寞
    2020-12-16 22:40

    Take a look at boost type traits library, in particular the function_traits template provides such functionality out of the box. If you cannot use boost, just download the code and read the sources for some insight into how it is done.

    Note that the functionality is based on types, not concrete functions, so you might need to add some extra code there.


    After doing some small tests, this might not be what you really need, and if it is the 'some extra code' will be non-trivial. The problem is that the function_traits template works on function signatures and not actual function pointers, so the problem has changed from 'get the return type from a function pointer' to 'get the signature from a function pointer' which is probably the hardest part there.

提交回复
热议问题