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

后端 未结 7 1703
温柔的废话
温柔的废话 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:31

    It's tricky because function names are expression not types - you need something like gcc's typeof. Boost's TypeOf is a portable solution that gets very close.

    However, if the code can be organised so that the work is done inside a function template to which Foo or Bar can be passed, there's a straight-forward answer:

    template 
    void test(R (*)())
    {
      R var1;
    }
    
    int main()
    {
      test(&Foo);
    }
    

提交回复
热议问题