如何在给定 T 和元素个数 n 的情况下构建 hana::tuple_t<T, T, T, ...>
How to build a hana::tuple_t<T, T, T, ...> given T and the number of elements 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> ) ) );
} );
远非简短易读...
正如@jv_ 所指出的,hana::replicate
完全可以做到这一点。
参考文档中的示例提供了有关如何实现此目的的足够信息:
// 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() { }
这似乎是一件相当基本的事情,所以我正在寻找一个或多或少简短、内置且易于阅读的解决方案。
我能想到的最短的事情是
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> ) ) );
} );
远非简短易读...
正如@jv_ 所指出的,hana::replicate
完全可以做到这一点。
参考文档中的示例提供了有关如何实现此目的的足够信息:
// 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() { }