![](/img/trans.png)
[英]How to pass a std::array to a function template which can accept std::vector
[英]How to make function template, which takes std::vector and pointer to function?
Function1 接受指向 std::vector(未指定類型)的指針和指向其他函數模板(Function2)的指針。 Function2 接受兩個對象(類型如 std::vector 類型)並返回 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
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));
我認為這是由線路引起的
template <typename tVector> void FunctionT(tVector* pVector, FunctionP pFunkcja);
FunctionP
是一個FunctionP
的模板。 您只能存儲指向模板函數特化的指針,而不能存儲實際的模板。
所以只需使用普通指針作為參數。 例如
bool Function2(int i1, int i2);
void Function1(std::vector<int>* v1, bool (*f)(int, int));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.