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
This commit is contained in:
maxbachmann 2019-05-16 08:41:29 +02:00 committed by Sébastien Rombauts
parent bb8c0ef0f2
commit 81913790f2
2 changed files with 14 additions and 34 deletions

View File

@ -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 <SQLiteCpp/Statement.h>
@ -21,23 +22,6 @@
namespace SQLite
{
/// implementation detail for variadic bind.
namespace detail {
template<class F, class ...Args, std::size_t ... I>
inline void invoke_with_index(F&& f, std::integer_sequence<std::size_t, I...>, const Args& ...args)
{
std::initializer_list<int>({ (f(I+1, args), 0)... });
}
/// implementation detail for variadic bind.
template<class F, class ...Args>
inline void invoke_with_index(F&& f, const Args& ... args)
{
invoke_with_index(std::forward<F>(f), std::index_sequence_for<Args...>(), 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<?");
@ -61,16 +45,13 @@ inline void invoke_with_index(F&& f, const Args& ... args)
template<class ...Args>
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<int>{
((void)s.bind(++pos, std::forward<decltype(args)>(args)), 0)...
};
detail::invoke_with_index(f, args...);
}
} // namespace SQLite
#endif // c++14
#endif // c++11

View File

@ -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 <cstdio>
#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<std::pair<int, std::string> > 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