Skip to content

Commit acdc2b0

Browse files
authored
Merge pull request #64 from aminroosta/move_ctor
Fixes #63
2 parents e34b4f6 + a3d45f7 commit acdc2b0

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

configure

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2752,14 +2752,14 @@ $as_echo "no" >&6; }
27522752
27532753
if test "" == ""
27542754
then
2755-
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking if compiler flag -std=c++11 works" >&5
2756-
$as_echo_n "checking if compiler flag -std=c++11 works... " >&6; }
2755+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking if compiler flag -std=c++14 works" >&5
2756+
$as_echo_n "checking if compiler flag -std=c++14 works... " >&6; }
27572757
else
27582758
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking " >&5
27592759
$as_echo_n "checking ... " >&6; }
27602760
fi
27612761
save_CXXFLAGS="$CXXFLAGS"
2762-
CXXFLAGS="$CXXFLAGS -std=c++11"
2762+
CXXFLAGS="$CXXFLAGS -std=c++14"
27632763
27642764
27652765

hdr/sqlite_modern_cpp.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,19 @@ namespace sqlite {
111111
class database_binder {
112112

113113
public:
114-
database_binder(database_binder&& other) = default;
115114
// database_binder is not copyable
116115
database_binder() = delete;
117116
database_binder(const database_binder& other) = delete;
118117
database_binder& operator=(const database_binder&) = delete;
119118

119+
database_binder(database_binder&& other) :
120+
_db(std::move(other._db)),
121+
_sql(std::move(other._sql)),
122+
_stmt(std::move(other._stmt)),
123+
_inx(other._inx), execution_started(other.execution_started) {
124+
_db = nullptr;
125+
_stmt = nullptr;
126+
}
120127

121128
void reset() {
122129
sqlite3_reset(_stmt.get());
@@ -253,7 +260,7 @@ namespace sqlite {
253260
~database_binder() noexcept(false) {
254261
/* Will be executed if no >>op is found, but not if an exception
255262
is in mid flight */
256-
if(!execution_started && !std::uncaught_exception()) {
263+
if(!execution_started && !std::uncaught_exception() && _stmt) {
257264
execute();
258265
}
259266
}

tests/mov_ctor.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Fixing https://github.com/aminroosta/sqlite_modern_cpp/issues/63
2+
#include <iostream>
3+
#include <cstdlib>
4+
#include <unistd.h>
5+
#include <sqlite_modern_cpp.h>
6+
#include <memory>
7+
using namespace sqlite;
8+
using namespace std;
9+
10+
struct dbFront {
11+
std::unique_ptr<database_binder> storedProcedure;
12+
database db;
13+
dbFront(): db(":memory:") {
14+
db << "CREATE TABLE tbl (id integer, name string);";
15+
// the temporary moved object should not run _execute() function on destruction.
16+
storedProcedure = std::make_unique<database_binder>(
17+
db << "INSERT INTO tbl VALUES (?, ?);"
18+
);
19+
}
20+
};
21+
22+
23+
int main() {
24+
25+
try {
26+
dbFront dbf;
27+
}
28+
catch(sqlite_exception e) {
29+
cout << "Unexpected error " << e.what() << endl;
30+
exit(EXIT_FAILURE);
31+
}
32+
catch(...) {
33+
cout << "Unknown error\n";
34+
exit(EXIT_FAILURE);
35+
}
36+
37+
cout << "OK\n";
38+
exit(EXIT_SUCCESS);
39+
}

0 commit comments

Comments
 (0)