【问题标题】:Passing a specific function to a template将特定函数传递给模板
【发布时间】:2019-04-10 14:01:30
【问题描述】:

我正在尝试将函数传递给模板,到目前为止我得到了这个:

template <typename T, typename U> void special_sort(std::vector<T>& container, std::vector<U>& ordering_container,
    std::function<std::vector<std::pair<unsigned int, unsigned int>>(std::vector<U>&)>& sort_function) {

    sort_function(ordering_container);
}

主要是这样声明的:

std::vector<std::pair<unsigned, unsigned>> sort_function(std::vector<int>&) {
    std::vector<std::pair<unsigned int, unsigned int>> a;
    return a;
}

int main() {
    std::vector<std::string> test_container = {"a", "b", "c"};
    std::vector<int> test_ordering_container = {4,7,2};

    auto a = sort_function(test_ordering_container);
    special_sort<std::string, int>(test_container, test_ordering_container, sort_function(test_ordering_container));
}

但我收到以下错误:

错误(活动)E0304 没有函数模板“special_sort”的实例与参数列表匹配 Objective_challenge C:\Users\mdp11\source\repos\Objective_challenge\Objective_challenge\main.cpp 21 个参数类型是:(std::vector&lt;std::string, @ 987654324@, std::vector&lt;int, std::allocator&lt;int&gt;&gt;, std::vector&lt;std::pair&lt;unsigned int, unsigned int&gt;, std::allocator&lt;std::pair&lt;unsigned int, unsigned int&gt;&gt;&gt;)

我做错了什么? 提前谢谢大家。

【问题讨论】:

  • 你真正想要实现什么?顺便说一句sort_function(test_ordering_container)这不是一个函数,但是你调用了这个函数,它返回一个向量,而一个向量不是函数;)
  • 我试图让那个模板函数接收 3 个参数,一个 T 的向量,一个 U 的向量,以及一个可以对第二个向量进行排序的函数,但我想在里面调用那个函数模板“special_sort”

标签: c++ function templates


【解决方案1】:

所以你这里有两个错误。

第一个是这样的:

special_sort<std::string, int>(
    test_container, test_ordering_container, 
    sort_function(test_ordering_container)
);//     --------^-----------------------^

您调用它并发送结果,而不是发送函数。

编译器抱怨向量不能转换为std::function。

special_sort<std::string, int>(
    test_container, test_ordering_container, 
    sort_function
);//        ^------ the function itself

第二个错误是通过可变引用获取函数。

函数的类型是not std::function。 sort_function的类型是函数指针类型。

std::function 类是任何可调用对象的多态包装器。因此 lambda 类型、函数对象类型、函数指针等都可以以类型擦除的方式包含在 std::function 中。

当将函数指针作为参数发送给您的函数时,它必须创建一个临时的std::function。但是这里的问题是临时不能绑定到可变引用,只能绑定常量:

template <typename T, typename U>
void special_sort(
    std::vector<T>& container,
    std::vector<U>& ordering_container,
    std::function<std::vector<std::pair<unsigned int, unsigned int>>(std::vector<U>&)> const& sort_function
) { //                            -----------------------------------------------------^

}

或者您也可以使用模板参数。在这种特殊情况下,您不需要运行时多态性和类型擦除。做 STL 所做的事情并简单地按值获取可调用对象:

template <typename T, typename U, typename F>
void special_sort(
    std::vector<T>& container,
    std::vector<U>& ordering_container,
    F sort_function
) {
    // much simpler
    // you can still do:
    // sort_function(ordering_container)
}

【讨论】:

    【解决方案2】:

    有两个错误。首先,使用这个:

    special_sort<std::string, int>(test_container, test_ordering_container, sort_function);
    

    而不是

    special_sort<std::string, int>(test_container, test_ordering_container, sort_function(test_ordering_container));
    

    您传递的是函数的结果而不是函数本身。

    还需要将special_sort的参数声明为

    const std::function<std::vector<std::pair<unsigned int, unsigned int>>(std::vector<U>&)>& sort_function
    

    而不是

    std::function<std::vector<std::pair<unsigned int, unsigned int>>(std::vector<U>&)>& sort_function
    

    因为sort_function指的是标准函数调用,所以需要转换成std::function才能调用special_sort。但是,这种隐式转换将创建一个不能作为引用传递的临时对象。但是临时对象接受 const 引用。

    【讨论】:

      猜你喜欢
      • 2011-11-15
      • 1970-01-01
      • 2017-12-25
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多