Making use of Scott Schurr's str_const (
https://github.com/boostcon/cppnow_presentations_2012/blob/master/wed/schurr...)
I
have a constexpr string.
class StrConst
{
public:
template
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;
using P2 = std::integral_constant;
using PosTuple = std::tuple;
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
using PosTuple = std::tuple...>;
How can I generate the sequence of Ns... required here using caretPos or
some other means?