2016-01-11 32 views
0

我试图在函子上使用std::result_of。为什么我会得到这些结果?std :: result_of在多态函子上

#include <typeinfo> 

struct my_logical_not { 
    template<typename A> 
    bool operator()(const A &value) const { 
     return !value; 
    } 
}; 

struct my_passthrough { 
    template<typename A> 
    A operator()(A &value) const { 
     return value; 
    } 
}; 

int main() { 


    // this prints 'b': 
    std::cout << typeid(typename std::result_of<my_logical_not(int)>::type).name() << std::endl; 

    // this does not compile: 
    // main.cpp:24:66: error: ‘type’ in ‘class std::result_of<my_passthrough(int)>’ does not name a type 

    std::cout << typeid(typename std::result_of<my_passthrough(int)>::type).name() << std::endl; 

    return 0; 
} 
+3

'my_passthrough(INT)'意味着你要(int)作为参数的右值,它不绑定到非常量左值引用 –

+0

提示:这里不需要'typenames' –

+0

我看到..将my_passthrough operator()更改为“operator() (const A&value)const“修正了它。谢谢! –

回答

1

正如彼得·Skotnicki在评论中指出,上面的代码工作,一旦my_passthrough改为采取一个const A的&代替&:

struct my_passthrough { 
     template<typename A> 
     A operator()(const A &value) const { 
      return value; 
     } 
    }; 
+2

或保持原样并使用'std :: result_of :: type'(请参见&符号) –

相关问题