You could write simple lambda expression like this:
auto d = [](sqlite3_stmt* stmt) { sqlite3_finalize(stmt); };
std::unique_ptr<sqlite3_stmt, decltype(d)> statement(new sqlite3_stmt, d);
Explanation
Custom deleter is created using lambda expression and stored in variable of automatically recognized type. decltype
is used to obtain type of expression (which in your case you must provide while creating unique_ptr
to feed it with your custom deleter).
Edit
There are two statement variables in your Datasource class code, you need to rename one of them. You cannot initialize d
and first statement
variables like this. I think there is no possibility to use auto in such context, but we can use std::function
. Include and replace auto d ...
with std::function<void(sqlite3_stmt*)> d;
. Initialize d
in constructor initialization list.
Change statement
to std::unique_ptr<int, decltype(d)> statement;
and also initialize it in constructor initialization list.
Datesource::Datasource() :
d([](sqlite3_stmt* stmt) { sqlite3_finalize(stmt); }),
statement(new sqlite3_stmt) { /* constructor code */ }