mardi 15 décembre 2015

Template Function C++ Sqlite3 Wrapper

In the process of creating a C++ wrapper for anSqlite3 database. Below are two functions that (working together) fill an std::vector with the results from a query; both of which were successful before I tried to template them. I am getting a compilation error (C2664 in VS) for char*[] to the char** which is the argument in the Sqlite3declaration.

I don't have great knowledge of C but from what I collect an array passed as a function argument is rewritten as a pointer, making char*[] and char** equivalent after the conversion, albeit the result is an rvalue. However, that would require that the vector to be a const reference, defeating the goal of populating it with results.

How can this be changed to correctly compile with a successful query? Any other advice or correction would be greatly appreciated. Thanks in advance.

template<class T, class A>
int exec_callback(void *ptr, int argc, char *argv[], char *names[]) {
    vector<T, A> *vec = reinterpret_cast<vector<T, A> *>(ptr);
    vec->push_back(T(int(argv[0]), string(argv[1]), atoi(argv[2]), atoi(argv[3]),
    atoi(argv[4]), atoi(argv[5])));
    return 0;
}

template<class T, class A>
void selectAllList(vector<T, A>& vec, sqlite3 *db, const char* statement) {
    char *errmsg = NULL;
    sqlite3_exec(db, statement, exec_callback, &vec, &errmsg);
    if (errmsg) {
        printf("error: %s!\n", errmsg);
        return;
    }
    else {
        fprintf(stdout, "Operation done successfully\n");
    }
    list(vec);
}

Aucun commentaire:

Enregistrer un commentaire