简体   繁体   English

如何制作函数模板,它需要 std::vector 和指向函数的指针?

[英]How to make function template, which takes std::vector and pointer to function?

Function1 takes pointer to std::vector (unspecified type) and pointer to other function template (Function2). Function1 接受指向 std::vector(未指定类型)的指针和指向其他函数模板(Function2)的指针。 Function2 takes two objects (type like std::vector type) and return bool. Function2 接受两个对象(类型如 std::vector 类型)并返回 bool。 How to make these templates?如何制作这些模板?

For Example:例如:

bool Function2(int i1, int i2);

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

I try:我尝试:

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

It gives:它给:

'FunctionP' is not a type
bool Function2(int a, int b)

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

call with specializations呼叫专业

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

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

I think this caused by the line我认为这是由线路引起的

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

FunctionP is a template of a function. FunctionP是一个FunctionP的模板。 You can only store pointers to specializations of template functions, but not the actual template.您只能存储指向模板函数特化的指针,而不能存储实际的模板。

So just use the normal pointers as an argument.所以只需使用普通指针作为参数。 eg例如

bool Function2(int i1, int i2);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM