奇怪的模板扣除 [英] Strange Template Deduction

查看:93
本文介绍了奇怪的模板扣除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个重复,但我还没有发现任何其他地方的问题。给定以下代码:

This may be a duplicate, but I haven't found the problem anywhere else yet. Given the following code:

#include <functional>
#include <algorithm>
#include <iostream>
#include <vector>

template<typename container_ty_>
auto where(container_ty_ const& V, std::function<bool(typename container_ty_::value_type const&)>&& comp) 
-> std::vector<std::reference_wrapper<typename container_ty_::value_type>> {
        std::vector<std::reference_wrapper<typename container_ty_::value_type>> cursor;

        for(typename container_ty_::value_type const& VAL : V)
                if(comp(VAL))
                        cursor.push_back(const_cast<typename container_ty_::value_type&>(VAL));

        return cursor;
}

int main(int argc, char** argv) {
        std::vector<int> tVect = {0, 5, 2, 1, 7, 9};

        //Why must std::vector<int> be passed...
        auto vec = where<std::vector<int>>(tVect, [](const int& V) -> bool { return V > 5; });

        std::for_each(vec.begin(), vec.end(), [] (int& v) { std::cout << v++ << std::endl; });
        std::cout << std::endl;
        std::for_each(tVect.begin(), tVect.end(), [](int& v) { std::cout << v << std::endl; });
}

vec 正在赋值,该函数似乎需要将 std :: vector< int> 传递给它,以便编译。如果现在我得到:

The line where vec is being assigned, the function where seems to need to have std::vector<int> passed into it in order to compile. if now i get:


testing.cpp:20:68:错误:没有匹配的函数调用'其中(std :: vector< int& V) - > bool {return(std :: vector< V> 5;});

我怀疑这是因为模板没有被正确推导对于的第二个参数,任何人都可以向我解释为什么,我似乎处于停顿...

I suspect this is because the template is not being deduced correctly for the second argument of where could anyone explain to me why, i seem to be at a standstill...

also:
命令行参数: g ++ testing.cpp -g -o testing -std = c ++ 11 -Wall

G ++版本: g ++(Ubuntu 4.8.4-2ubuntu1〜14.04.3)4.8.4

推荐答案

您可能对稍微更灵活的版本感兴趣:

You may be interested in a slightly more flexible version, which:


  1. p>保留源向量中的值的常数(constivity)(取决于向量本身的常数)
  1. preserves constness of the values in the source vector (depending on the constness of the vector itself)

使用任何函子,不需要 std :: function

Takes any functor, no need for a std::function

-

#include <algorithm>
#include <iostream>
#include <vector>

template<typename container_ty_, class Comp>
auto where(container_ty_& V, Comp&& comp)
{
    using value_type = typename container_ty_::value_type;
    using reference =
    std::conditional_t<
      std::is_const<container_ty_>::value,
        std::reference_wrapper<const value_type>,
        std::reference_wrapper<value_type>
    >;

    std::vector<reference> cursor;

    for(auto& VAL : V)
        if(comp(VAL))
            cursor.push_back(VAL);

    return cursor;
}

int main(int argc, char** argv) {
    std::vector<int> tVect = {0, 5, 2, 1, 7, 9};

    //Why must std::vector<int> be passed...
    auto vec = where(tVect, [](const int& V) -> bool { return V > 5; });

    std::for_each(vec.begin(), vec.end(), [] (int& v) { std::cout << v++ << std::endl; });
    std::cout << std::endl;
    std::for_each(tVect.begin(), tVect.end(), [](const int& v) { std::cout << v << std::endl; });
}

这篇关于奇怪的模板扣除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆