samedi 4 novembre 2017

Wrapping ellipsis-based function with variadic template function

I am trying to wrap a function 'prepared(.)' from an external library (libpqxx). Said function accepts a variable number of arguments. This is achieved by using multiple () in succession. One () pr. argument, like so:

pqxx::connection connection(connection_str);
connection.prepare("update_person", "UPDATE person SET name = $1 WHERE id = $2 AND version = $3");
pqxx::work w(connection);

// The following executes prepared statement.
// The number of arguments is variable. Notice
// the syntax with multiple () in succession...
w.prepared("update_person")("Jack")(1)(0).exec();

I am trying to wrap the last function by using a variadic template function, like so:

template<typename... T>
pqxx::result exec_prepared(const std::string& name, const T&... args) const {
    return w.prepared(name)(args...).exec();
}

...but it does not work. The code compiles, but I get a runtime error saying that the number of arguments do not match the expected number of arguments given the prepared-sql-statement.

Could someone please clarify how wrapping of this type of function is done using variadic template? Thanks!

Aucun commentaire:

Enregistrer un commentaire