Fixed-sized alternative to initializer_list

1,002 views
Skip to first unread message

jba...@jbat.es

unread,
Jan 24, 2014, 8:36:40 PM1/24/14
to std-pr...@isocpp.org
I've been playing around with types that extend the functionality of std::array. I want to add more constructors, but I also want to retain the aggregate initialization syntax, e.g.:
my_cool_array_type<float, 4> a = {1, 2, 3, 4};

Neither variadic templates nor initializer_list yield satisfactory results. A good discussion as to why can be found in the following SO question: http://stackoverflow.com/questions/21342545/implementing-stdarray-like-constructors-in-other-classes

initializer_list comes close, but it's not well-optimized for generic fixed-sized containers. Is anyone opposed to a fixed-sized alternative to initializer_list (maybe call it initializer_array)? I'm interested in writing a proposal.

jba...@jbat.es

unread,
Jan 24, 2014, 8:43:28 PM1/24/14
to std-pr...@isocpp.org, jba...@jbat.es
Just realized I didn't really give any details for what I want to propose!

What I want would be essentially the same as initializer_list, but have a second template parameter specifying a size. It would then only match braced-init-lists of the specified size. Maybe it would also match the interface of std::array and/or include some easy way to initialize std::arrays or C-style arrays with it.

Alex B

unread,
Jan 25, 2014, 2:45:57 PM1/25/14
to std-pr...@isocpp.org, jba...@jbat.es
I just posted on SO a solution that should do the trick in C++11.

The cleanest solution would probably to have fixed size parameter packs:

Unfortunately, there does not seem to be a proposal in sight for what is suggested in the original post of the linked thread.

Another solution to this problem might be the ability to generate packs "on the fly" with a packification operator or pack aliases which were discussed in the two following threads:

I don't have any proposal planned about this for the moment and I don't know if Richard has plans concerning this.

jba...@jbat.es

unread,
Jan 25, 2014, 5:12:46 PM1/25/14
to std-pr...@isocpp.org, jba...@jbat.es
Those fixed size parameter packs look like they would solve my problem perfectly, but I can't help but wonder why initializer_list was implemented as a type instead of a new syntax. Anyone know the reasoning behind that? I imagine whatever it is, it applies here too.

Billy O'Neal

unread,
Jan 25, 2014, 5:25:13 PM1/25/14
to std-proposals, jba...@jbat.es
I'm confused why you need initializer list for this. If you want a fixed number of parameters, isn't any old POD struct just fine for what you're trying to accomplish?

For instance:


struct foo { int a, b, c; };
void i_only_take_three_braced_parameters(foo f)
{
    // ...
}
int main()
{
    i_only_take_three_braced_parameters({1, 2, 3});
    i_only_take_three_braced_parameters({1, 2, 3, 4});
}

What is the overall issue you're trying to solve by limiting the number of initializers?

--
 
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

jba...@jbat.es

unread,
Jan 26, 2014, 9:28:20 PM1/26/14
to std-pr...@isocpp.org, jba...@jbat.es
I'm mostly interested in mimicking the syntax for aggregate initialization for array types on custom types. Again, I want to be able to write something like the following:

my_cool_array_type<float, 4> a = {1, 2, 3, 4};

I'm aware it's easy to do with other syntax, but I really like the above syntax. Also, with PODs like the one you suggest, it's possible to give less than enough values, e.g:

i_only_take_three_braced_parameters({1, 2});

would be accepted whereas what I'm proposing would allow exactly 3 values; no more or less.
Reply all
Reply to author
Forward
0 new messages