5

Dies scheint wie eine ziemlich grundlegende Sache zu tun, so suche ich nach einem mehr-or- weniger kurze, eingebaute und leicht lesbare Lösung.
Das kürzeste, was ich zu denken geschaffen istWie man einen Hana :: Tuple_t <T, T, T, ...> T und die Anzahl der Elemente n

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>))); 
     }); 

, die von kurz und lesbar weit ist ...

+1

'hana :: replicate' angemessen erscheint. – llonesmiz

Antwort

3

Wie @jv_ hana::replicate kann das tun genau aufgezeigt.
Das Beispiel in der Referenzdokumentation gibt genug Informationen darüber, wie dieses Ziel zu erreichen:

// 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() { } 
Verwandte Themen