im fairly new to programming and C++. I have a function that i want to accept function-pointers with templated values as argument. Here is what i mean...
i have this function:
template<typename... ColumnTypes, typename... ParameterTypes>
void query(std::function<void(bool success, ozo::rows_of<ColumnTypes...>& results)> callback, const
std::string& query, ParameterTypes&& ... parameters);
"ozo::rows_of" is alias for:
template <typename ... Ts>
std::vector<std::tuple<Ts...>>
I want each query to be provided with a callback, this callback will neeed to be able to accept different types. eg. "ColumnTypes"
What i've tried:
void myfunc(bool succeeded, ozo::rows_of<int>& results)
{
//code
}
postgres_caller->query(myfunc, "SELECT length FROM this_table WHERE id > $1 AND id < $2;", 11, 14);
result:
.cpp:241:26: error: no matching member function for call to 'query'
postgres_caller->query(myfunc, "SELECT length FROM this_table WHERE id > $1 AND id < $2;", 11, 14);
~~~~~~~~~~~~~~~~~^~~~~
.h:165:22: note: candidate template ignored: could not match 'function<void (bool, vector<tuple<type-parameter-0-0...>, allocator<tuple<type-parameter-0-0...> > > &)>' against 'void (*)(bool, std::vectorstd::tuple<int, std::allocatorstd::tuple<int > > &)' void PostgresCaller::query(std::function<void(bool success, ozo::rows_of<ColumnTypes...>& results)> callback, const std::string& query, ParameterTypes&& ... parameters)
I also tried with lambda:
postgres_caller->query([](bool succeeded, ozo::rows_of<int>& results)
{
//code
}, "SELECT length FROM this_table WHERE id > $1 AND id < $2;", 11, 14);
result:
error: no matching member function for call to 'query'
postgres_caller->query([](bool succeeded, ozo::rows_of<int>& results)
~~~~~~~~~~~~~~~~~^~~~~
.h:165:22: note: candidate template ignored: could not match 'function<void (bool, vector<tuple<type-parameter-0-0...>, allocator<tuple<type-parameter-0-0...> > > &)>' against '(lambda at .cpp:241:32)' void PostgresCaller::query(std::function<void(bool success, ozo::rows_of<ColumnTypes...>& results)> callback, const std::string& query, ParameterTypes&& ... parameters) ^
Is this doable and how can it be done? much appreciated. /John
query
expectsstd::function
as the first parameter. You are passing things to it that are not specializations ofstd::function
. Template parameter deduction doesn't consider implicit conversions. You'd have to wrap the callback into a suitablestd::function
at the call site. Or else redesignquery
to accept an arbitrary callable, if you have control over it. – Igor Tandetnik