Variadic template for multidimensional std::array - c++

We can alias a multidimensional array like this:
template<typename T, size_t size1, size_t size2>
using myArray = std::array<std::array<T, size2>, size1>;
But this only allows us a predefined number of dimensions. Is there a way to turn this into a variadic template, so that we could write any of those:
myArray<int, 2, 2, 2> arr3d;
myArray<int, 2, 2, 2, 2> arr4d;
I've tried a few things but wasn't completely satisfied with any of them.
This:
template<typename T, size_t size>
using myArray<T, size> = std::array<T, size>;
template<typename T, size_t size, size_t... more>
using myArray = std::array<myArray<T, more...>, size>;
doesn't even compile because template specializations are apparently not allowed with alias templates.
This is currently my best solution but deletes all constructors of std::array which I would like to keep:
template<typename T, size_t size, size_t... more>
struct myArray : public std::array<myArray<T, more...>, size> {};
template<typename T, size_t size>
struct myArray<T, size> : public std::array<T, size>{};
And with this solution I would always have to write ".internal" in front of every array access:
template<typename T, size_t size, size_t... more>
struct myArr {
std::array<myArr<T, more...>, size> internal;
};
template<typename T, size_t size>
struct myArr<T, size> {
std::array<T, size> internal;
};
So could anyone think of a solution like the second but where I could keep the constructors? I can't.

I would suggest simply multiplying the dimensions together and having a single std::array. E.g.: std::array<int, D0 * D1 * D2 * D3>. You can then provide utility functions or a wrapper class to convert a multi-dimensional index into one-dimensional.
Anyway...
Here's a simple recursive solution that uses explicit template specialization:
template <typename T, std::size_t... Ds>
struct nested_array;
template <typename T, std::size_t D>
struct nested_array<T, D>
{
using type = std::array<T, D>;
};
template <typename T, std::size_t D, std::size_t... Ds>
struct nested_array<T, D, Ds...>
{
using type = std::array<typename nested_array<T, Ds...>::type, D>;
};
static_assert(std::is_same_v<
typename nested_array<int, 1, 2, 3>::type,
std::array<std::array<std::array<int, 3>, 2>, 1>
>);
live example on wandbox
Here's a solution based on variable template partial specialization:
template <typename T>
struct t { using type = T; };
template <typename T>
using unwrap = typename T::type;
template <typename T, std::size_t... Ds>
constexpr auto nested_array = t<void>{};
template <typename T, std::size_t D>
constexpr auto nested_array<T, D> = t<std::array<T, D>>{};
template <typename T, std::size_t D, std::size_t... Ds>
constexpr auto nested_array<T, D, Ds...> =
t<std::array<unwrap<decltype(nested_array<T, Ds...>)>, D>>{};
static_assert(std::is_same_v<
unwrap<decltype(nested_array<int, 1, 2, 3>)>,
std::array<std::array<std::array<int, 3>, 2>, 1>
>);
live example on wandbox

You might use extra layer:
template<typename T, size_t size, size_t... more>
struct myArray_impl
{
using type = std::array<typename myArray_impl<T, more...>::type, size>;
};
template<typename T, size_t size>
struct myArray_impl<T, size>
{
using type = std::array<T, size>;
};
template<typename T, size_t size, size_t... more>
using myArray = typename myArray_impl<T, size, more...>::type;

Related

How can I unpack multidimensional c-style array type and declare std::array instance?

I'm writing a template that can convert a c style multidimensional array to a std array and declare it.
get c-style array matrix
template <typename, typename IS = std::index_sequence<>>
struct unpacked_array_type_impl
{
using type = IS;
};
template <typename T, std::size_t ... I>
struct unpacked_array_type_impl<T*, std::index_sequence<I...>>
: public unpacked_array_type_impl<T, std::index_sequence<0u, I...>>
{ };
template <typename T, std::size_t N, std::size_t ... I>
struct unpacked_array_type_impl<T[N], std::index_sequence<I...>>
: public unpacked_array_type_impl<T, std::index_sequence<I..., N>>
{ };
template<typename T>
struct unpacked_array_type
{
using type = typename unpacked_array_type_impl<T>::type;
};
std array declaration
template<typename T, size_t size, size_t... more>
struct myArray_impl
{
using type = std::array<typename myArray_impl<T, more...>::type, size>;
};
template<typename T, size_t size>
struct myArray_impl<T, size>
{
using type = std::array<T, size>;
};
template<typename T, size_t size, size_t... more>
using myArray = typename myArray_impl<T, size, more...>::type;
Below is the code I want to achieve.
using example_type = int[4][10];
using real_type = std::remove_all_extents<example_type>::type
myArray<real_type, unpacked_array_type<example_type>::type> this_would_be_std_arr;
But I got C2993 error.
The problem is that std::integer_sequence cannot expand to the variable argument of myArray template.
I would appreciate if you can help me.
First template return std::index_sequence, whereas the second take a list of number.
You might use std::index_sequence for both:
template<typename T, typename Seq>
struct myArray_impl;
template<typename T, size_t size, size_t... more>
struct myArray_impl<T, std::index_sequence<size, more...>>
{
using type = std::array<typename myArray_impl<T, std::index_sequence<more...>>::type, size>;
};
template<typename T, size_t size>
struct myArray_impl<T, std::index_sequence<size>>
{
using type = std::array<T, size>;
};
template<typename T, typename Seq>
using myArray = typename myArray_impl<T, Seq>::type;
Demo.
Instead of trying to build a stack of array extents with std::index_sequence and apply them to a template parameter pack, why not just define your specialization of myArray_impl to recursively deduce each extent from typename T?
template <typename T>
struct myArray_impl
{
using type = typename std::remove_cv<T>::type;
};
template <typename T, std::size_t N>
struct myArray_impl<T[N]>
{
using type = std::array<typename myArray_impl<T>::type, N>;
};
template <typename T>
using myArray = typename myArray_impl<T>::type;
Then you get the following example type aliases:
myArray<const int[4]> // std::array<int, 4>>
myArray<float[4][10]> // std::array<std::array<float, 10>, 4>>

Is there any way to access everything except the last template parameter?

It is possible to use template parameters pack as follows:
template <int T1, int... Ts>
struct Test {
static constexpr int sizes[] = {Ts...};
};
template <int T1, int... Ts>
constexpr int Test<T1, Ts...>::sizes[];
However, as it is detailed here, the template parameter pack must be the last template parameter. Hence, we cannot have a code such as this:
template <int T1, int... Ts, int Tn>
struct Test {
static constexpr int sizes[] = {Ts...};
Foo<Ts...> foo;
};
template <int T1, int... Ts, int Tn>
constexpr int Test<T1, Ts..., Tn>::sizes[];
In many cases, we need to have access to the last element of a set of template parameters. My question is, what's the best practice for realizing the above code?
Edit:
This is not duplicate of this question. I am trying to get everything except the last parameter (not the last parameter itself), since I need to define Foo as follows:
Foo<Ts...> foo;
You could go with the standard method of using std::index_sequence
template<template<auto...> typename Tmp, size_t... Is, typename... Args>
constexpr auto take_as(std::index_sequence<Is...>, Args...)
{
using Tup = std::tuple<Args...>;
return Tmp<std::tuple_element_t<Is, Tup>{}...>{};
}
template<auto... Vals>
struct except_last
{
template<template<auto...> typename Tmp>
using as = decltype(take_as<Tmp>(std::make_index_sequence<sizeof...(Vals) - 1>{},
std::integral_constant<decltype(Vals), Vals>{}...));
};
Which you use as
using F = except_last<1, 2, 3, 4>::as<Foo>; // F is Foo<1, 2, 3>
This is both easier to implement and read, but you potentially get O(n) instantiation depth. If you are obsessed with efficiency, you could do O(1) instantiation depth by abusing fold expressions
template<typename T>
struct tag
{
using type = T;
};
template<typename F, typename... Ts>
using fold_t = decltype((F{} + ... + tag<Ts>{}));
template<size_t N, typename... Ts>
struct take
{
template<typename T>
auto operator+(tag<T>) -> take<N - 1, Ts..., T>;
};
template<typename... Ts>
struct take<0, Ts...>
{
template<template<auto...> typename Tmp>
using as = Tmp<Ts{}...>;
template<typename T>
auto operator+(tag<T>) -> take<0, Ts...>;
};
template<auto... Vals>
struct except_last
{
template<template<auto...> typename Tmp>
using as = fold_t<take<sizeof...(Vals) - 1>,
std::integral_constant<decltype(Vals), Vals>...>::template as<Tmp>;
};
What's the most efficient way to access the last template parameter?
You could use a little helper to convert the parameter pack into an array.
template<int... Args>
struct pack {
static constexpr std::array as_array{ Args... };
};
You can then get the last argument with array indexing:
template <int T1, int... Ts>
struct Test {
static constexpr int last = pack<Ts...>::as_array[sizeof...(Ts) - 1];
integer_sequence is a way:
template <typename SeqN, typename Seq> struct TestImpl;
template <int... Ns, std::size_t ... Is>
struct TestImpl<std::integer_sequence<int, Ns...>, std::index_sequence<Is...>>
{
private:
using SeqTuple = std::tuple<std::integral_constant<int, Ns>...>;
public:
static constexpr int sizes[] = {std::tuple_element_t<Is, SeqTuple>::value...};
Foo<std::tuple_element_t<Is, SeqTuple>::value...> foo;
};
template <int N1, int N2, int... Ns> // At least 2 numbers
using Test = TestImpl<std::integer_sequence<int, N1, N2, Ns...>,
std::make_index_sequence<1 + sizeof...(Ns)>>;
Demo

Avoiding struct in variadic template function

Trying to search for this answer, I noticed that the titles of the questions
on variadic template functions are very uninformative when searching on
SO. You only known that the question is about variadic template. Hopefully my
question wasn't asked before and the title will help people finding it.
So, I want to make a function which has a variadic template parameter. More
precisely, as an example, let say I want to have a compile time permutation of
an array. Here is a working code:
template <typename... Ts> struct Sequence {};
template <typename T, unsigned Size, typename... SeqTis> struct Permute;
template <typename T, unsigned Size, typename... SeqTis>
struct Permute<T, Size, Sequence<SeqTis...> > {
using type = typename std::array<T, Size>;
constexpr static type permute(const type ar) {
return { (ar[SeqTis::value])... };
}
};
Then the following is perfectly legal:
using T0 = std::integral_constant<int, 0>;
using T1 = std::integral_constant<int, 1>;
using T2 = std::integral_constant<int, 2>;
using Perm120 = Permute<int, 3, Sequence<T1, T2, T0> >;
using arr3 = Perm120::type;
constexpr arr3 ar {5,7,2};
constexpr arr3 arPerm = Perm120::permute(ar);
I'm now trying to avoid using a structure so I wrote the following:
template <typename T, unsigned Size, typename... SeqTis>
constexpr typename std::array<T, Size>
permutefun<T, Size, Sequence<SeqTis...> >(const typename std::array<T, Size> ar) {
return { (ar[SeqTis::value])... };
}
And GCC refuse it saying that
essai.cpp:19:11: error: expected initializer before ‘<’ token
permutefun<T, Size, Sequence<SeqTis...> >(const typename std::array<T, Size> ar) {
^
Why is it so ?
To add to previous answers, you also need to have a convenient syntax for calling permutefun, but the current template parameters
template <typename T, unsigned Size, typename... SeqTis>
are not convenient because you'd have to call
permutefun <int, 3, T1, T2, T0>(ar);
A solution is to deduce arguments in two steps:
#include <array>
template <typename... Ts> struct Sequence {};
template <typename... SeqTis, typename T, unsigned long Size>
constexpr std::array<T, Size>
permutefun(Sequence<SeqTis...>, const std::array<T, Size> ar) {
return { (ar[SeqTis::value])... };
}
template <typename Seq, typename T, unsigned long Size>
constexpr std::array<T, Size>
permutefun(const std::array<T, Size> ar) {
return permutefun(Seq(), ar);
}
int main ()
{
using T0 = std::integral_constant<int, 0>;
using T1 = std::integral_constant<int, 1>;
using T2 = std::integral_constant<int, 2>;
using Perm120 = Sequence<T1, T2, T0>;
using arr3 = std::array <int, 3>;
constexpr arr3 ar = {5,7,2};
constexpr arr3 arPerm = permutefun <Perm120>(ar);
}
Now arguments T, Size appear last so are automatically deduced by the input array. Argument Seq comes first, but to deduce its "unpacked" parameters SeqTis... you need a call to a second overload of permutefun. This allows the convenient syntax
permutefun <Perm120>(ar);
The 2nd overload may be useful by itself, because it allows the alternative syntax
Perm120 perm;
permutefun(perm, ar);
Also note that std::array::operator[] is constexpr only in C++14. For instance, this does not compile with Clang 3.3 and -std=c++11.
Your syntax looks like an attempt at a partial function specialisation, but that's not what you want, so that's not what you should write. Since partial function specialisations aren't supported, the compiler gets confused by the unexpected <.
template <typename T, unsigned Size, typename... SeqTis>
constexpr std::array<T, Size>
permutefun(const std::array<T, Size> ar) {
// ^ removed the <...>
return { (ar[SeqTis::value])... };
}
Also, you don't need typename here, as std::array<T, Size> is already known to be a type. It's fine to leave it in, but it works just as well without it.
Do you mean:
template <typename T, unsigned Size, typename... SeqTis>
constexpr std::array<T, Size>
permutefun(const typename std::array<T, Size> ar) {
return { (ar[SeqTis::value])... };
}
partial template specialization is not possible for function.

Error spliting an std::index_sequence

I'm trying to split an index_sequence into two halves. For that, I generate an index_sequence with the lower half and use it to skip the leading elements on the full index_sequence. The following is a minimal test case that represents what I'm trying to achieve:
template <int ...I>
struct index_sequence {};
template <int ...I, int ...J>
void foo(index_sequence<I...>, index_sequence<I..., J...>)
{}
int main()
{
foo(index_sequence<0>{}, index_sequence<0, 1>{});
}
I've tried this with the latest versions of Clang, GCC and MSVC, and they all fail to deduce J.... Is this allowed by the standard? If not, why and what would be a good way to achieve my intent?
If what you want is to split a std::index_sequence instead of removing the common prefix of two std::index_sequences, I think you can benefit from an implementation of slice and using it to split a std::index_sequence into pieces.
I'm going to omit the implementation of std::index_sequence and friends, since you can refer to the paper, N3658, and a sample implementation here.
make_index_range
To implement slice, we'll use a helper called make_integer_range. We want a std::index_sequence generator which gives us [Begin, End) instead of [0, End). Leveraging std::make_integer_sequence, we get:
template <typename T, typename Seq, T Begin>
struct make_integer_range_impl;
template <typename T, T... Ints, T Begin>
struct make_integer_range_impl<T, std::integer_sequence<T, Ints...>, Begin> {
using type = std::integer_sequence<T, Begin + Ints...>;
};
/* Similar to std::make_integer_sequence<>, except it goes from [Begin, End)
instead of [0, End). */
template <typename T, T Begin, T End>
using make_integer_range = typename make_integer_range_impl<
T, std::make_integer_sequence<T, End - Begin>, Begin>::type;
/* Similar to std::make_index_sequence<>, except it goes from [Begin, End)
instead of [0, End). */
template <std::size_t Begin, std::size_t End>
using make_index_range = make_integer_range<std::size_t, Begin, End>;
slice
Since we don't have a std::get-like functionality for std::index_sequence or a variadic template pack, we just build a temporary std::array to get us std::get. Then explode the array with only the slice we want.
template <std::size_t... Indices, std::size_t... I>
constexpr decltype(auto) slice_impl(
std::index_sequence<Indices...>,
std::index_sequence<I...>) {
using Array = std::array<std::size_t, sizeof...(Indices)>;
return std::index_sequence<std::get<I>(Array{{Indices...}})...>();
}
template <std::size_t Begin, std::size_t End, std::size_t... Indices>
constexpr decltype(auto) slice(std::index_sequence<Indices...> idx_seq) {
return slice_impl(idx_seq, make_index_range<Begin, End>());
}
split_at
One example of using the slice we just built is to write a split_at function. We specify the index at which we want to split the std::index_sequence, and return a pair of std::index_sequences split at the given index.
template <std::size_t At, std::size_t... Indices>
constexpr decltype(auto) split_at(index_sequence<Indices...> idx_seq) {
return std::make_pair(slice<0, At>(idx_seq),
slice<At, sizeof...(Indices)>(idx_seq));
}
Examples of split_at:
static_assert(std::is_same<
decltype(split_at<2>(index_sequence<1, 4, 2>())),
std::pair<index_sequence<1, 4>, index_sequence<2>>>(), "");
static_assert(std::is_same<
decltype(split_at<1>(index_sequence<1, 4, 2, 3>())),
std::pair<index_sequence<1>, index_sequence<4, 2, 3>>>(), "");
14.8.2.5/9 ... If the template argument list of P contains a pack expansion that is not the last template argument, the entire template argument list is a non-deduced context...
Thus, when comparing index_sequence<I..., J...> with index_sequence<0, 1>{}, neither I... nor J... can be deduced.
To get suffix, you may use something like:
template<int ... I> struct get_suffix_helper {
template<int ... J> static index_sequence<J...> foo(index_sequence<I..., J...>);
};
template<typename T1, typename T2> struct get_suffix;
template<int ... Is1, int ... Is2>
struct get_suffix<index_sequence<Is1...>, index_sequence<Is2...>> :
public decltype(get_suffix_helper<Is1...>::foo(std::declval<index_sequence<Is2...>>())) {};
static_assert(std::is_base_of<index_sequence<>,
get_suffix<index_sequence<1, 2>,
index_sequence<1, 2>>>::value, "error");
static_assert(std::is_base_of<index_sequence<42>,
get_suffix<index_sequence<1, 2>,
index_sequence<1, 2, 42>>>::value, "error");
Or, with some error check:
template <typename T1, typename T2> struct get_suffix;
template<int ...Is>
struct get_suffix<index_sequence<>, index_sequence<Is...>>
{
typedef index_sequence<Is...> type;
static const bool valid = true;
};
template<int ...Is>
struct get_suffix<index_sequence<Is...>, index_sequence<>>
{
typedef void type;
static const bool valid = false;
};
template<>
struct get_suffix<index_sequence<>, index_sequence<>>
{
typedef index_sequence<> type;
static const bool valid = true;
};
template<int N, int ...Is, int... Js>
struct get_suffix<index_sequence<N, Is...>, index_sequence<N, Js...>>
{
typedef typename get_suffix<index_sequence<Is...>, index_sequence<Js...>>::type type;
static const bool valid = get_suffix<index_sequence<Is...>, index_sequence<Js...>>::valid;
};
template<int N1, int N2, int ...Is, int... Js>
struct get_suffix<index_sequence<N1, Is...>, index_sequence<N2, Js...>>
{
typedef void type;
static const bool valid = false;
};
static_assert(std::is_same<index_sequence<>,
get_suffix<index_sequence<1, 2>,
index_sequence<1, 2>>::type>::value, "error");
static_assert(!get_suffix<index_sequence<1, 2, 42>, index_sequence<1, 2>>::valid, "error");
static_assert(!get_suffix<index_sequence<0, 2, 1>, index_sequence<0, 1, 2>>::valid, "error");
static_assert(std::is_same<index_sequence<42>,
get_suffix<index_sequence<1, 2>,
index_sequence<1, 2, 42>>::type>::value, "error");
Not an answer, but a workaround: recursively trim off the leading elements a la:
template <typename, typename> struct remove_prefix;
template <std::size_t... I>
struct remove_prefix<index_sequence<>, index_sequence<I...>> {
using type = index_sequence<I...>;
};
template <std::size_t First, std::size_t... I, std::size_t... J>
struct remove_prefix<index_sequence<First, I...>,
index_sequence<First, J...>> {
using type = typename remove_prefix<index_sequence<I...>,
index_sequence<J...>>::type;
};
Demo at Coliru.
I needed to split an index_sequence into a head and tail at a particular point and this was the implementation that I came up with:
template<size_t N, typename Lseq, typename Rseq>
struct split_sequence_impl;
template<size_t N, size_t L1, size_t...Ls, size_t...Rs>
struct split_sequence_impl<N,index_sequence<L1,Ls...>,index_sequence<Rs...>> {
using next = split_sequence_impl<N-1,index_sequence<Ls...>,index_sequence<Rs...,L1>>;
using head = typename next::head;
using tail = typename next::tail;
};
template<size_t L1, size_t...Ls, size_t...Rs>
struct split_sequence_impl<0,index_sequence<L1,Ls...>,index_sequence<Rs...>> {
using tail = index_sequence<Ls...>;
using head = index_sequence<Rs...,L1>;
};
template<typename seq, size_t N>
using split_sequence = split_sequence_impl<N-1,seq,empty_sequence>;
template<typename seq, size_t N>
using sequence_head_t = typename split_sequence<seq,N>::head;
template<typename seq, size_t N>
using sequence_tail_t = typename split_sequence<seq,N>::tail;

Fill container with template parameters

I want to fill the template parameters passed to a variadic template into an array with fixed length. For that purpose I wrote the following helper function templates
template<typename ForwardIterator, typename T>
void fill(ForwardIterator i) { }
template<typename ForwardIterator, typename T, T head, T... tail>
void fill(ForwardIterator i) {
*i = head;
fill<ForwardIterator, T, tail...>(++i);
}
the following class template
template<typename T, T... args>
struct params_to_array;
template<typename T, T last>
struct params_to_array<T, last> {
static const std::size_t SIZE = 1;
typedef std::array<T, SIZE> array_type;
static const array_type params;
private:
void init_params() {
array_type result;
fill<typename array_type::iterator, T, head, tail...>(result.begin());
return result;
}
};
template<typename T, T head, T... tail>
struct params_to_array<T, head, tail...> {
static const std::size_t SIZE = params_to_array<T, tail...>::SIZE + 1;
typedef std::array<T, SIZE> array_type;
static const array_type params;
private:
void init_params() {
array_type result;
fill<typename array_type::iterator, T, last>(result.begin());
return result;
}
};
and initialized the static constants via
template<typename T, T last>
const typename param_to_array<T, last>::array_type
param_to_array<T, last>::params =
param_to_array<T, last>::init_params();
and
template<typename T, T head, T... tail>
const typename param_to_array<T, head, tail...>::array_type
param_to_array<T, head, tail...>::params =
param_to_array<T, head, tail...>::init_params();
Now the array
param_to_array<int, 1, 3, 4>::params
is a std::array<int, 3> and contains the values 1, 3 and 4. I think there must be a simpler way to achieve this behavior. Any suggestions?
Edit: As Noah Roberts suggested in his answer I modified my program like the following: I wrote a new struct counting the elements in a parameter list:
template<typename T, T... args>
struct count;
template<typename T, T head, T... tail>
struct count<T, head, tail...> {
static const std::size_t value = count<T, tail...>::value + 1;
};
template<typename T, T last>
stuct count<T, last> {
static const std::size_t value = 1;
};
and wrote the following function
template<typename T, T... args>
std::array<T, count<T, args...>::value>
params_to_array() {
std::array<T, count<T, args...>::value> result;
fill<typename std::array<T, count<T, args...>::value>::iterator,
T, args...>(result.begin());
return result;
}
Now I get with
params_to_array<int, 10, 20, 30>()
a std::array<int, 3> with the content 10, 20 and 30. Any further suggestions?
There is no need to count the number of types in a parameter pack manually, thats what the sizeof... operator is for. Additionally i'd make the iterator type for fill() deducible, there is no need to specify it explicitly:
template<typename T, typename FwdIt>
void fill(FwdIt it) { }
template<typename T, T head, T... tail, typename FwdIt>
void fill(FwdIt it) {
*it = head;
fill<T, tail...>(++it);
}
template<class T, T... args>
std::array<T, sizeof...(args)> params_to_array() {
std::array<T, sizeof...(args)> a;
fill<T, args...>(a.begin());
return a;
};
Parameter packs however are also expandable in initializer-list contexts, which makes fill() redundant:
template<class T, T... args>
std::array<T, sizeof...(args)> params_to_array() {
std::array<T, sizeof...(args)> a = {{args...}};
return a;
};
The only reason I can see for a specialization for terminus in param_to_array is this line:
static const std::size_t SIZE = params_to_array<T, tail...>::SIZE + 1;
Since your params_to_array metafunction creates the array though you're going to end up instantiating arrays of size N, N-1, ...., 1. Thus I think your object could use some help from composition and the single responsibility rule. Create another metafunction that can count the elements in a parameter list and use it instead of this method. Then you can get rid of this recursion in params_to_array at the least.