|
Boost Users : |
Subject: Re: [Boost-users] [hana] create aribtrary tuples of integral_constants
From: d25fe0be_at_[hidden]
Date: 2017-04-19 14:20:00
Iâm not clear why hana is mentioned in the title.. So I might have misunderstood the question..
If what you want is a function which takes a StrConst as an argument and returns a PosTuple<...>, I donât think thereâs a way to do that. Since a StrConst loses its constexpr-ness as soon as itâs passed as an argument, the functionâs return type (PosTuple<...>) cannot be deduced from it.
If youâre not sticked to functions, I think itâs possible to traverse the characters in StrConst one-by-one, and build the tuple on-the-fly.
Hereâs my code, hope it helps: https://godbolt.org/g/aAcdjS
> On 10 Apr 2017, at 09:01, Steve Lorimer via Boost-users <boost-users_at_[hidden]> wrote:
>
> Making use of Scott Schurr's str_const (https://github.com/boostcon/cppnow_presentations_2012/blob/master/wed/schurr_cpp11_tools_for_class_authors.pdf?raw=true) 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?
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> https://lists.boost.org/mailman/listinfo.cgi/boost-users
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net