SRombauts / SQLiteCpp

SQLiteC++ (SQLiteCpp) is a smart and easy to use C++ SQLite3 wrapper.
http://srombauts.github.io/SQLiteCpp
MIT License
2.21k stars 510 forks source link

Implement move constructors #190

Closed bstaletic closed 5 years ago

bstaletic commented 5 years ago

Things like Database and Statement leverage RAII and own some kind of object, so it makes sense to make them non-copyable. However moving them around should be supported. Consider the following:

class DatabaseWrapper {
        SQLite::Database db;
        SQLite::Statement query;
public:
        DatabaseWrapper() : 
            db(":memory:", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE)
            // The following line doesn't work - "Table" still doesn't exist
            // , query(db, "select * from Table where X = ?")
        {
                SQLite::Transaction tr(db);
                db.exec("create table Table (X varchar)");
                tr.commit();
                // Can't use copy assignment to innitialize `query`
                // query = SQLite::Statement(db, "select * from Table where X = ?");
                // No move constructor provided
                // query = std::move( SQLite::Statement(db, "select * from Table where X = ?") );
        }
};

I was trying to use this to save time constructing the Statement in a tight loop.

SRombauts commented 5 years ago

Yes, it totally make sense!

At the beginning of the project I was using VS2010 Pre C++11 but now I could even make a V2 full C++11 kwas always the plan)

SRombauts commented 5 years ago

Also, Database received a move ctor recently :)

SRombauts commented 5 years ago

Ok, so the first thing to note is that the Database move ctor introduced in #157 is guarded by #if __cplusplus >= 201103L, which sadly does not work with Visual Studio.

Second thing is there is no unit tests nor examples to this.

I'll work to improve this asap, and then complete other classes with such move semantics as appropriate.