0

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;
}
4

1 に答える 1

1

Piotr Skotnicki がコメントで指摘したように、my_passthrough が A& の代わりに const A& を取るように変更されると、上記のコードは機能します。

   struct my_passthrough {
       template<typename A>
       A operator()(const A &value) const {
           return value;
       }
   };
于 2016-01-11T17:59:31.687 に答える