Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a hana::tuple_t<T, T, T, ...> given T and the number of elements n

This seems like a fairly basic thing to do, so I am looking for a more-or-less short, builtin and easily readable solution.
The shortest thing I managed to conceive is

hana::unfold_left<hana::tuple_tag>( hana::int_c<n>, [] ( auto count ) {
            return hana::if_( count == hana::int_c<0>, hana::nothing,
                             hana::just( hana::make_pair( count - hana::int_c<1>,
                                                        hana::type_c<T> ) ) );
        } );

which is far from being short and readable...

like image 833
iolo Avatar asked Oct 19 '22 00:10

iolo


1 Answers

As pointed out by @jv_ hana::replicate can do exactly that.
The example in the reference documentation gives enough information on how to achieve this:

// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#include <boost/hana/equal.hpp>
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/optional.hpp>
#include <boost/hana/replicate.hpp>
#include <boost/hana/tuple.hpp>

namespace hana = boost::hana;
static_assert(hana::replicate<hana::tuple_tag>('x', hana::size_c<2>) == hana::make_tuple('x', 'x'), "");
// Of course, there can't be more than one element in an `optional`.
static_assert(hana::replicate<hana::optional_tag>('x', hana::size_c<2>) == hana::just('x'), "");
int main() { }
like image 167
iolo Avatar answered Nov 01 '22 07:11

iolo