怎么使用boost::function作为sqlite3_exec的回调函数,给出示例代码
时间: 2023-04-01 19:03:50 浏览: 155
可以使用boost::bind将boost::function转换为C语言风格的回调函数,示例代码如下:
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <sqlite3.h>
int callback(void *data, int argc, char **argv, char **azColName) {
boost::function<int(void*, int, char**, char**)>* f = static_cast<boost::function<int(void*, int, char**, char**)>*>(data);
return (*f)(data, argc, argv, azColName);
}
int main() {
sqlite3* db;
sqlite3_open("test.db", &db);
boost::function<int(void*, int, char**, char**)> f = [](void* data, int argc, char** argv, char** azColName) {
for (int i = ; i < argc; i++) {
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
return ;
};
char* errmsg;
sqlite3_exec(db, "SELECT * FROM test", &callback, new boost::function<int(void*, int, char**, char**)>(f), &errmsg);
sqlite3_close(db);
return ;
}
阅读全文