如何制作函数模板,它使std :: vector和指针起作用?

时间:2015-11-01 20:48:06

标签: c++ templates

Function1将指针指向std :: vector(未指定类型)和指向其他函数模板(Function2)的指针。 Function2接受两个对象(类型为std :: vector type)并返回bool。如何制作这些模板?

例如:

bool Function2(int i1, int i2);

void Function1(std::vector<int>* v1, Function2);

我试试:

template <typename type> bool FunctionP(type, type);
template <typename tVector> void FunctionT(tVector* pVector, FunctionP pFunkcja);

它给出了:

'FunctionP' is not a type

2 个答案:

答案 0 :(得分:1)

bool Function2(int a, int b)

template <typename T>
void Function1(std::vector<T>* vector, std::function < bool(T, T)> callback)

使用专业化调用

std::vector<int> vec{1,2,3,4,5};

Function1<int>(&vec, std::bind(Function2, std::placeholders::_1, std::placeholders::_2));

答案 1 :(得分:0)

我认为这是由行

引起的
template <typename tVector> void FunctionT(tVector* pVector, FunctionP pFunkcja);

FunctionP是函数的模板。您只能存储指向模板函数特化的指针,而不能存储实际模板。

所以只需使用普通指针作为参数。 e.g。

bool Function2(int i1, int i2);

void Function1(std::vector<int>* v1, bool (*f)(int, int));
相关问题