I'm writing an open source sqlite interface library (mSqliteCpp for instance) that uses c++ classes and types to correctly manage sqlite databases.
So far the library is heavily using TMP to build SQL strings, but I found a possible issue that may somehow affect the efficiency of the library.
I'm using classes to manage Fields definitions, classes definitions, queries and statements. Basically to define a table or a SELECT statement, one defines the fields using proper FieldDef<T> objects, then pass them to a statement builder that returns the correctly formatted SQL statement.
For example:
auto fldId = sqlite::makeFieldDef("id", sqlite::FieldType::Integer());
auto fldName = sqlite::makeFieldDef("name", sqlite::FieldType::Text());
auto fldValue = sqlite::makeFieldDef("value", sqlite::FieldType::Real());
sqlite::statements::Select select("Table", fldId, fldName, fldValue);
ASSERT_EQ(select.string(), "SELECT id,name,value FROM Table;");
Note that each field is strongly typed by the FieldType type passed to the makeFieldDef function, but different fields with similar type can be exchanged. For this reason the SQL statement in the ASSERT call is built at runtime.
I would like to have them built at compile time, at least under certain conditions. For example, developer could use a different class or maybe the constexpr keyword. But at this time I've no idea if this could be achieved.
What are the possible patterns? Can strings be statically built through TMP? I'm using C++11/14.
Thank you.
what I'm looking for is a solution that will make most of the computation with strings at compile time [...] At the moment I'm just courious about how this could be implemented.
Not an answer to your sqlite question but... if your "computation with strings at compile time" are simple as concatenation... just to play with constexpr and template metaprogramming...
std::string isn't constexpr but std::array can be.
So you can define a fake string as an alias for an array of chars.
template <std::size_t N>
using fakeStr = std::array<char, N>;
You can convert a string literal to a fake string as follows (with an helper function and playing with type traits)
template <std::size_t N, std::size_t ... Is>
constexpr fakeStr<sizeof...(Is)> mFSh (char const (& str)[N],
std::index_sequence<Is...> const &)
{ return { { str[Is]... } }; }
template <std::size_t N>
constexpr auto makeFakeStr (char const (& str)[N])
{ return mFSh(str, std::make_index_sequence<N-1>{}); }
The fake string concatenation is simple as follows
template <std::size_t N1, std::size_t ... I1s,
std::size_t N2, std::size_t ... I2s>
constexpr fakeStr<N1+N2> cFSh (fakeStr<N1> const & s1,
std::index_sequence<I1s...> const &,
fakeStr<N2> const & s2,
std::index_sequence<I2s...> const &)
{ return { { s1[I1s]..., s2[I2s]... } }; }
template <std::size_t N1, std::size_t N2>
constexpr auto concatFakeStr (fakeStr<N1> const & s1, fakeStr<N2> const & s2)
{ return cFSh(s1, std::make_index_sequence<N1>{},
s2, std::make_index_sequence<N2>{}); }
and a constexpr comparison can be done as follows (with C++17 the helper function can be simpler)
template <std::size_t N1, std::size_t N2, std::size_t ... Is>
constexpr bool cmpFSh (fakeStr<N1> const & s1,
fakeStr<N2> const & s2,
std::index_sequence<Is...> const &)
{
using unused = bool[];
bool ret { true };
(void) unused { true, ret &= s1[Is] == s2[Is] ... };
return ret;
}
template <std::size_t N1, std::size_t N2>
constexpr bool compareFakeStr (fakeStr<N1> const & s1, fakeStr<N2> const & s2)
{ return (N1 == N2) && cmpFSh(s1, s2, std::make_index_sequence<N1>{}); }
If you want a std::string from a fakeStr, the following code (not constexpr, obviously) can help
template <std::size_t N, std::size_t ... Is>
std::string fFSh (fakeStr<N> const & s, std::index_sequence<Is...> const &)
{ return { s[Is]..., '\0' }; }
template <std::size_t N>
auto fromFakeString (fakeStr<N> const & s)
{ return fFSh(s, std::make_index_sequence<N>{}); }
The following is a full (C++14) working example
#include <array>
#include <string>
#include <iostream>
#include <type_traits>
template <std::size_t N>
using fakeStr = std::array<char, N>;
template <std::size_t N, std::size_t ... Is>
constexpr fakeStr<sizeof...(Is)> mFSh (char const (& str)[N],
std::index_sequence<Is...> const &)
{ return { { str[Is]... } }; }
template <std::size_t N>
constexpr auto makeFakeStr (char const (& str)[N])
{ return mFSh(str, std::make_index_sequence<N-1>{}); }
template <std::size_t N1, std::size_t ... I1s,
std::size_t N2, std::size_t ... I2s>
constexpr fakeStr<N1+N2> cFSh (fakeStr<N1> const & s1,
std::index_sequence<I1s...> const &,
fakeStr<N2> const & s2,
std::index_sequence<I2s...> const &)
{ return { { s1[I1s]..., s2[I2s]... } }; }
template <std::size_t N1, std::size_t N2>
constexpr auto concatFakeStr (fakeStr<N1> const & s1, fakeStr<N2> const & s2)
{ return cFSh(s1, std::make_index_sequence<N1>{},
s2, std::make_index_sequence<N2>{}); }
template <std::size_t N1, std::size_t N2, std::size_t ... Is>
constexpr bool cmpFSh (fakeStr<N1> const & s1,
fakeStr<N2> const & s2,
std::index_sequence<Is...> const &)
{
using unused = bool[];
bool ret { true };
(void) unused { true, ret &= s1[Is] == s2[Is] ... };
return ret;
}
template <std::size_t N1, std::size_t N2>
constexpr bool compareFakeStr (fakeStr<N1> const & s1, fakeStr<N2> const & s2)
{ return (N1 == N2) && cmpFSh(s1, s2, std::make_index_sequence<N1>{}); }
template <std::size_t N, std::size_t ... Is>
std::string fFSh (fakeStr<N> const & s, std::index_sequence<Is...> const &)
{ return { s[Is]..., '\0' }; }
template <std::size_t N>
auto fromFakeString (fakeStr<N> const & s)
{ return fFSh(s, std::make_index_sequence<N>{}); }
int main()
{
constexpr auto f1 = makeFakeStr("abcd");
constexpr auto f2 = makeFakeStr("xyz");
constexpr auto f12 = concatFakeStr(f1, f2);
constexpr auto f3 = makeFakeStr("abcdxyz");
static_assert( true == compareFakeStr(f12, f3), "!" );
static_assert( false == compareFakeStr(f12, f1), "!" );
auto s12 = fromFakeString(f12);
std::cout << s12 << std::endl;
}
I repeat: just to play.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With