Boost logo

Boost Users :

Subject: [Boost-users] [Tuple] Help getting started with Tuple-based meta-programming
From: Dominique Devienne (ddevienne_at_[hidden])
Date: 2009-03-20 15:46:23

Hi All. I outline below code I keep writing which I'd like to replace
with a sql_get_rows template function which would use the meta-type
information bundled in the tuple declaration to automatically make as
many calls to the get<T> function based on the tuple length, and
"pack" the result of these calls into a tuple instance added to a
container (vector in this case, but if it could be generalized to any
container, all the better, although I'd be happy just with vector).

I have experience with basic template stuff, and a little less basic
stuff like CRTP, enable_if, and the like, but meta-programming is
still new to me, which is why I'm seeking advice from experts to find
out whether the below is : 1) possible, 2) would perform ok, and 3)
where I should start looking to see examples doing this particular
"tuple-enrolling" or perhaps even pseudo code outlining how one would
go about implementing this, provided answers to (1) and (2) are
positive of course. I suspect mpl and phoenix would need to be used,
from the reading I've done, but the path to follow is quite unclear to
me still. Any help in this matter would be appreciated.

Thanks, --DD

// Context: retrieving all the rows (result set) from a given SQL query.
// pseudo code of boilerplate code I keep repeating right now
struct { int id; std::string name; } Row;
void doit() {
    ResultSet rset = sql_exec("select id, name from t");
    std::vector<Row> rows;
    while (rset.hasMoreRows()) {
        Row row; = rset.get<int>(0); = rset.get<const char*>(1);
    // process rows

// Code I'd like to write instead
void doit() {
    typedef boost::tuple<int, std::string> Row;
    std::vector<Row> rows;
    sql_get_rows("select id, name from t", rows); // optional arg to
get only first N elements
    // process rows

PS: returning the vector would be better, but because of the copy
involved I guess I must pass it as a non-const ref instead, to fill it
up. Will the new move semantic from the upcoming standard allow a
function to return the vector without the copy? Just curious.

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at