简体   繁体   中英

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). Function2 takes two objects (type like std::vector type) and return 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. 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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