From 81913790f23a721719700af205ff3b48df6a119c Mon Sep 17 00:00:00 2001 From: maxbachmann <44199644+maxbachmann@users.noreply.github.com> Date: Thu, 16 May 2019 08:41:29 +0200 Subject: [PATCH] Update VariadicBind.h for C++11 instead of C++14 (#196) * Update VariadicBind.h * replace c++14 by c++11 * activate tests for c++11 * replace string_literals for c++11 support --- include/SQLiteCpp/VariadicBind.h | 33 +++++++------------------------- tests/VariadicBind_test.cpp | 15 +++++++-------- 2 files changed, 14 insertions(+), 34 deletions(-) diff --git a/include/SQLiteCpp/VariadicBind.h b/include/SQLiteCpp/VariadicBind.h index 24e5e5f..2b9853c 100644 --- a/include/SQLiteCpp/VariadicBind.h +++ b/include/SQLiteCpp/VariadicBind.h @@ -5,13 +5,14 @@ * * Copyright (c) 2016 Paul Dreik (github@pauldreik.se) * Copyright (c) 2016-2019 Sebastien Rombauts (sebastien.rombauts@gmail.com) + * Copyright (c) 2019 Maximilian Bachmann (github@maxbachmann) * * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt * or copy at http://opensource.org/licenses/MIT) */ #pragma once -#if (__cplusplus >= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) ) // c++14: Visual Studio 2015 +#if (__cplusplus >= 201103L) || ( defined(_MSC_VER) && (_MSC_VER >= 1800) ) // c++11: Visual Studio 2013 #include @@ -21,23 +22,6 @@ namespace SQLite { - -/// implementation detail for variadic bind. -namespace detail { -template -inline void invoke_with_index(F&& f, std::integer_sequence, const Args& ...args) -{ - std::initializer_list({ (f(I+1, args), 0)... }); -} - -/// implementation detail for variadic bind. -template -inline void invoke_with_index(F&& f, const Args& ... args) -{ - invoke_with_index(std::forward(f), std::index_sequence_for(), args...); -} - -} // namespace detail /// @endcond /** @@ -45,7 +29,7 @@ inline void invoke_with_index(F&& f, const Args& ... args) * * This takes care of incrementing the index between each calls to bind. * - * This feature requires a c++14 capable compiler. + * This feature requires a c++11 capable compiler. * * \code{.cpp} * SQLite::Statement stm("SELECT * FROM MyTable WHERE colA>? && colB=? && colC void bind(SQLite::Statement& s, const Args& ... args) { - static_assert(sizeof...(args) > 0, "please invoke bind with one or more args"); - - auto f=[&s](std::size_t index, const auto& value) - { - s.bind(index, value); + int pos = 0; + (void)std::initializer_list{ + ((void)s.bind(++pos, std::forward(args)), 0)... }; - detail::invoke_with_index(f, args...); } } // namespace SQLite -#endif // c++14 +#endif // c++11 diff --git a/tests/VariadicBind_test.cpp b/tests/VariadicBind_test.cpp index 70a83c8..8dcb2e7 100644 --- a/tests/VariadicBind_test.cpp +++ b/tests/VariadicBind_test.cpp @@ -5,6 +5,7 @@ * * Copyright (c) 2016 Paul Dreik (github@pauldreik.se) * Copyright (c) 2016-2019 Sebastien Rombauts (sebastien.rombauts@gmail.com) + * Copyright (c) 2019 Maximilian Bachmann (github@maxbachmann) * * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt * or copy at http://opensource.org/licenses/MIT) @@ -18,7 +19,7 @@ #include -#if (__cplusplus >= 201402L) || ( defined(_MSC_VER) && (_MSC_VER >= 1900) ) // c++14: Visual Studio 2015 +#if (__cplusplus >= 201103L) || ( defined(_MSC_VER) && (_MSC_VER >= 1800) ) // c++11: Visual Studio 2013 TEST(VariadicBind, invalid) { // Create a new database SQLite::Database db(":memory:", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE); @@ -53,9 +54,7 @@ TEST(VariadicBind, invalid) { // make sure the content is as expected { - using namespace std::string_literals; - - SQLite::Statement query(db, "SELECT id, value FROM test ORDER BY id"s); + SQLite::Statement query(db, std::string{"SELECT id, value FROM test ORDER BY id"}); std::vector > results; while (query.executeStep()) { const int id = query.getColumn(0); @@ -64,9 +63,9 @@ TEST(VariadicBind, invalid) { } EXPECT_EQ(std::size_t(3), results.size()); - EXPECT_EQ(std::make_pair(1,""s), results.at(0)); - EXPECT_EQ(std::make_pair(2,"two"s), results.at(1)); - EXPECT_EQ(std::make_pair(3,"three"s), results.at(2)); + EXPECT_EQ(std::make_pair(1,std::string{""}), results.at(0)); + EXPECT_EQ(std::make_pair(2,std::string{"two"}), results.at(1)); + EXPECT_EQ(std::make_pair(3,std::string{"three"}), results.at(2)); } } -#endif // c++14 +#endif // c++11