Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Combinatorial arg explosion for function accepting initializer list literals

31 views
Skip to first unread message

Alf P. Steinbach

unread,
Mar 30, 2017, 7:40:30 AM3/30/17
to
In C++03 perfect forwarding of a sequence of arguments of arbitrary
types, caused a combinatorial explosion in const versus non-const.

In C++11 and later even just writing a function that /accepts/ arbitrary
argument types, when it's desirable to accept std::initializer_list
literals such as `{1, 2, 3}`, causes a combinatorial explosion in
initializer_list (which must known to the compiler as such) versus others.

E.g. in order to be able to call

multi( {1, 2, 3}, {5, 6, 7} )

with up to 3 arguments, a bunch of silly overloads are apparently needed:

template< class... Collections >
class Multidimensional_view_
{
public:
Multidimensional_view_( Collections const&... )
{}
};

template< class... Collections >
inline $f make_multidimensional_view( ref_<const Collections>... c )
-> Multidimensional_view_< Collections... >
{ return Multidimensional_view_< Collections... >{ c... }; }

template< class... Collections >
inline auto multi( ref_<Collections>... c )
{ return make_multidimensional_view( c... ); }

template< class Value >
// i
inline auto multi(
ref_<const initializer_list<Value>> list
)
{ return make_multidimensional_view( list ); }

template< class Collection, class Value >
// ci
inline auto multi(
ref_<const Collection> c,
ref_<const initializer_list<Value>> list
)
{ return make_multidimensional_view( c, list ); }

template< class Value, class Collection >
// ic
inline auto multi(
ref_<const initializer_list<Value>> list,
ref_<const Collection> c
)
{ return make_multidimensional_view( list, c ); }

template< class Value_1, class Value_2 >
// ii
inline $f multi(
ref_<const initializer_list<Value_1>> list_1,
ref_<const initializer_list<Value_2>> list_2
)
{ return make_multidimensional_view( list_1, list_2 ); }

template< class Collection_1, class Collection_2, class Value >
// cci
inline auto multi(
ref_<const Collection_1> c_1,
ref_<const Collection_2> c_2,
ref_<const initializer_list<Value>> list
)
{ return make_multidimensional_view( c_1, c_2, list ); }

template< class Collection_1, class Value, class Collection_2 >
// cic
inline auto multi(
ref_<const Collection_1> c_1,
ref_<const initializer_list<Value>> list,
ref_<const Collection_2> c_2
)
{ return make_multidimensional_view( c_1, list, c_2 ); }

template< class Collection, class Value_1, class Value_2 >
// cii
inline auto multi(
ref_<const Collection> c,
ref_<const initializer_list<Value_1>> list_1,
ref_<const initializer_list<Value_2>> list_2
)
{ return make_multidimensional_view( c, list_1, list_2 ); }

template< class Value, class Collection_1, class Collection_2 >
// icc
inline auto multi(
ref_<const initializer_list<Value>> list,
ref_<const Collection_1> c_1,
ref_<const Collection_2> c_2
)
{ return make_multidimensional_view( list, c_1, c_2 ); }

template< class Value_1, class Collection, class Value_2 >
// ici
inline auto multi(
ref_<const initializer_list<Value_1>> list_1,
ref_<const Collection> c,
ref_<const initializer_list<Value_2>> list_2
)
{ return make_multidimensional_view( list_1, c, list_2 ); }

template< class Value_1, class Value_2, class Collection >
// iic
inline auto multi(
ref_<const initializer_list<Value_1>> list_1,
ref_<const initializer_list<Value_2>> list_2,
ref_<const Collection> c
)
{ return make_multidimensional_view( list_1, list_2, c ); }

template< class Value_1, class Value_2, class Value_3 >
// iii
inline auto multi(
ref_<const initializer_list<Value_1>> list_1,
ref_<const initializer_list<Value_2>> list_2,
ref_<const initializer_list<Value_3>> list_3
)
{ return make_multidimensional_view( list_1, list_2, list_3 ); }

How can this be avoided?


Cheers!,

- Alf

Alf P. Steinbach

unread,
Mar 30, 2017, 7:44:15 AM3/30/17
to
On 30-Mar-17 1:40 PM, Alf P. Steinbach wrote:
> [snip]
>

Sorry about the formatting, the non-aligned comments: it's Thunderbird
messing things up with its flowed format implementation.

Cheers!,

- Alf

Juha Nieminen

unread,
Apr 3, 2017, 2:04:57 AM4/3/17
to
Alf P. Steinbach <alf.p.stein...@gmail.com> wrote:
> inline $f make_multidimensional_view( ref_<const Collections>... c )

Perhaps use C++, for starters?

Alf P. Steinbach

unread,
Apr 3, 2017, 4:12:02 AM4/3/17
to
Oh sorry. It is C++ but with use of a library. I think the code example
can be sufficiently understood to illustrate the problem, without me
using much time on replacing the library stuff with plain direct C++.

Here just replace `$f` with `auto`, and the `ref_` is self-explanatory I
should think. :)

Cheers!,

- Alf

0 new messages