2016-01-11 4 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)' означает, что вы хотите rvalue типа int в качестве аргумента, который не связывается с ссылкой на не константу lvalue –

+0

Совет: вам не нужны «typenames» здесь –

+0

Я вижу .. изменение my_passthrough operator() как оператора A() (const A & value) const "исправил его. Благодаря! –

ответ

1

Как Петр Скотницкого отметил в комментариях, приведенный выше код работает один раз my_passthrough изменяется взять константный & вместо А &:

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

или оставить его как есть и использовать 'std :: result_of :: type' (см. Амперсанд) –

Смежные вопросы