std :: invoke_result_t编译时语法错误

时间:2018-04-29 20:33:50

标签: c++ iterator

我正在创建一个带有iterable(一个容器)的函数,它的begin和end方法返回迭代器,其解除引用可以通过传递的lambda来修改。它听起来很复杂,但我试图做一些类似Python的超级整洁

modified_iterator = (fn(x) for x in my_iterator)

代码:

    template<typename Container, typename Fn>
    class IterableWrapper {
    public:
        template<typename Iterator>
        class IteratorWrapper : public Iterator {
        public:
            template<typename ...Args>
            explicit IteratorWrapper(Fn fn, Args ... args) : Iterator(args ...), fn(fn) {}

            //typename std::invoke_result_t<Fn, typename std::invoke_result_t<typename Iterator::operator*>> not working
            typename std::invoke_result_t<Fn,uint64_t> operator* () const {
                return fn(Iterator::operator*());
            }
        private:
            Fn fn;
        };

        IterableWrapper(const Container&& c, Fn&& fn) : c(std::move(c)), fn(std::forward<Fn>(fn)) {}

        auto begin() const {
            return IteratorWrapper<decltype(c.begin())>(fn, c.begin());
        };

        auto end() const {
            return IteratorWrapper<decltype(c.end())>(fn, c.end());
        };

    private:
        Container c;
        Fn fn;
    };

    template<typename C, typename Fn>
    auto wrap_iterable(C& c, Fn&& fn) = delete;

    template<typename C, typename Fn>
    auto wrap_iterable(C&& c, Fn&& fn) {
        return IterableWrapper<C, Fn>(std::move(c), std::forward<Fn>(fn));
    }

期望的用法:

    auto new_iterable = wrap_iterable(std::vector<uint64_t>{1,2,3,4}, [](auto&& item) { return std::pow(item, 2); });

我不想在uint64_t的invoke_result中对IteratorWrapper::operator*进行硬编码。它应该是基类中operator*的返回类型(即模板化类型Iterator)。

但是用注释掉的返回类型替换硬编码头会给我一个编译错误。新标题:

typename std::invoke_result_t<Fn, typename std::invoke_result_t<typename Iterator::operator*>> operator* () const

错误:

In file included from /Users/adam/school/cpp/invertedindex/main.cpp:203:0:
/Users/adam/school/cpp/invertedindex/inverted_index.hpp:61:105: error: template argument 1 is invalid
             typename std::invoke_result_t<Fn, typename std::invoke_result_t<typename Iterator::operator*>> operator* () const {  // typename std::invoke_result_t<Fn, typename std::invoke_result_t<typename Iterator::operator*>> not compiling??
                                                                                                         ^~
/Users/adam/school/cpp/invertedindex/inverted_index.hpp:61:121: error: template argument 2 is invalid
             typename std::invoke_result_t<Fn, typename std::invoke_result_t<typename Iterator::operator*>> operator* () const {  // typename std::invoke_result_t<Fn, typename std::invoke_result_t<typename Iterator::operator*>> not compiling??
                                                                                                                         ^~~~~
/Users/adam/school/cpp/invertedindex/inverted_index.hpp:61:127: error: expected identifier before '{' token
             typename std::invoke_result_t<Fn, typename std::invoke_result_t<typename Iterator::operator*>> operator* () const {  // typename std::invoke_result_t<Fn, typename std::invoke_result_t<typename Iterator::operator*>> not compiling??
                                                                                                                               ^
/Users/adam/school/cpp/invertedindex/inverted_index.hpp:61:127: error: expected unqualified-id before '{' token
In file included from /Users/adam/school/cpp/invertedindex/main.cpp:203:0:

1 个答案:

答案 0 :(得分:2)

您期望typename Iterator::operator*产生什么?一种?一个函数指针?一种函数指针?

没有名为operator*的成员类型。可能有一个以这种方式命名的函数。你的意思是把它的返回类型提供给std::invoke_result_t吗?

如果是这样,那将是:

std::invoke_result_t<decltype(&Iterator::operator*), Iterator>

但你可以通过一个简单的declval缩短它:

decltype(*std::declval<Iterator>())