Making use of Scott Schurr's str_const
I have a constexpr
string.
class StrConst
{
public:
template<size_t N>
constexpr StrConst(const char (&str)[N])
: str_(str)
, len_(N - 1)
{
static_assert(N > 1, "not a string");
}
constexpr operator const char*() const
{
return str_;
}
constexpr size_t size() const
{
return len_;
}
constexpr char operator[] (size_t i) const
{
return i < len_ ? str_[i] : throw std::out_of_range("invalid index");
}
private:
const char* const str_;
const size_t len_;
};
I have another constexpr
function which returns the position of the first caret found in a string, starting from position n:
constexpr int caretPos(const StrConst& str, size_t n = 0)
{
if (n == str.size())
return -1;
if (str[n] == '^')
return n;
return caretPos(str, n+1);
}
I can use the results of caretPos
to create a typedef for a std::tuple
of std::integral_constants
, where the size of the tuple is the number of carets found in the string, and each tuple element is an integral constant whose value is the position of the caret in the string.
Here I manually construct this tuple:
int main()
{
constexpr StrConst s("hello^world^");
constexpr int pos1 = caretPos(s);
constexpr int pos2 = caretPos(s, pos1+1);
using P1 = std::integral_constant<int, pos1>;
using P2 = std::integral_constant<int, pos2>;
using PosTuple = std::tuple<P1, P2>;
static_assert(std::tuple_element_t<0, PosTuple>::value == 5, "");
static_assert(std::tuple_element_t<1, PosTuple>::value == 11, "");
}
Question:
I would now like to generalise this for any input string with any number of carets.
template<size_t... Ns>
using PosTuple = std::tuple<std::integral_constant<int, Ns>...>;
How can I generate the sequence of Ns...
required here using caretPos
or some other means?
Working example
Intriguing question.
Avoiding the use of StrConst
, in the following example you can see a way to get a std::tuple<std::integral_constant<std::size_t, Pos1>, std::integral_constant<std::size_t, Pos2>, ...>
type.
First of all, use arrayConverter()
to convert a char const *
in a std::array<char, N>
(lc
, in the following code); second: use tupleIndexGenerator<lc.size(), lc, '^'>::type
to get the requested type.
So, if "hello^world^"
is the string, from tupleIndexGenerator<lc.size(), lc, '^'>::type
you get std::tuple<std::integral_constant<std::size_t, 5U>, std::integral_constant<std::size_t, 11U>>
The code
#include <iostream>
#include <array>
#include <tuple>
template <typename, typename>
struct typeConcat;
template <typename T0, template <typename ...> class C, typename ... Ts>
struct typeConcat<T0, C<Ts...>>
{ using type = C<T0, Ts...>; };
template <std::size_t N, std::array<char, N> const & A, char CH,
std::size_t Pos = 0U, bool EQ = ((Pos < N) && (A.at(Pos) == CH))>
struct tupleIndexGenerator;
template <std::size_t N, std::array<char, N> const & A, char CH>
struct tupleIndexGenerator<N, A, CH, N, false>
{ using type = std::tuple<>; };
template <std::size_t N, std::array<char, N> const & A, char CH,
std::size_t Pos>
struct tupleIndexGenerator<N, A, CH, Pos, false>
{ using type = typename tupleIndexGenerator<N, A, CH, Pos+1U>::type; };
template <std::size_t N, std::array<char, N> const & A, char CH,
std::size_t Pos>
struct tupleIndexGenerator<N, A, CH, Pos, true>
{ using type = typename typeConcat<
std::integral_constant<std::size_t, Pos>,
typename tupleIndexGenerator<N, A, CH, Pos+1U>::type>::type; };
template <typename T, size_t N, size_t ... Is>
constexpr auto arrayConverter (T const (&arr)[N],
std::index_sequence<Is...> const &)
{ return std::array<T, N> { { arr[Is]... } }; }
template <typename T, size_t N>
constexpr auto arrayConverter (T const (&arr)[N])
{ return arrayConverter(arr, std::make_index_sequence<N>{}); }
constexpr auto lc = arrayConverter("hello^world^");
int main ()
{
static_assert(std::is_same<
typename tupleIndexGenerator<lc.size(), lc, '^'>::type,
std::tuple<std::integral_constant<std::size_t, 5U>,
std::integral_constant<std::size_t, 11U>>>::value,
"!");
}
Here's an example using Boost.Hana (which advertises C++14). It results in a tuple of integral constants, which is directly what was asked for.
#include <cstddef>
#include <utility>
#include <boost/hana.hpp>
namespace hana = boost::hana;
using namespace boost::hana::literals;
template<typename Str, int... Is>
constexpr auto unfilteredCaretsImpl(Str str, std::integer_sequence<int, Is...>) {
return hana::make_tuple(boost::hana::if_(str[hana::int_c<Is>] == hana::char_c<'^'>, hana::just(hana::int_c<Is>), hana::nothing)...);
}
template<typename Str>
constexpr auto unfilteredCarets(Str str) {
return unfilteredCaretsImpl(str, std::make_integer_sequence<int, hana::length(str)>{});
}
template<typename Str>
constexpr auto allCarets(Str str) {
auto unfiltered = unfilteredCarets(str);
auto filtered = hana::filter(unfiltered, [](auto opt) { return opt != hana::nothing; });
return hana::transform(filtered, [](auto opt) { return opt.value(); });
}
int main() {
constexpr auto carets = allCarets(BOOST_HANA_STRING("hello^world^"));
static_assert(hana::length(carets) == std::size_t{2});
static_assert(carets[0_c] == 5);
static_assert(carets[1_c] == 11);
}
Most of the work is getting around the fact that each value is a different type. Hana encodes the values as part of the type, which means it's possible to use a parameter in a constant expression. For example, a Hana string is of type String<'a', 'b', 'c'>
for some artificial String
. That means that when using a parameter to compare a character, that character is known as part of the type. This is different from Scott's string, which goes full on constexpr
and cannot lift a parameter to a constant expression within the function.
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