
I'd like to propose a new function for the Boost Array library named make_array(). The function constructs a fixed size array given N arguments and is similar to the make_pair() and make_tuple() functions. Interface: The make_array() function provides the following interface: template<class T> boost::array<T, N> make_array(...) where the '...' indicates N arguments. If no type T is passed explicitly it is deduced from the type of the first argument. The function requires that all of the arguments must be convertible (via static_cast<>) to type T. Example: // create 2-component array of type int boost::array<int, 2> a = boost::make_array(4, 2); // create 3-component array of type float given int arguments boost::array<float, 3> b = boost::make_array<float>(1, 2, 3); // create 4-component array of doubles using C++11 auto auto c = boost::make_array(2.0, 4.0, 6.0); Implementation: make_array() has two separate implementations: one for compilers with support for C++11 variadic templates and a fallback implementation for those which do not. The C++11 variadic template implementation supports any arbitrary number of arguments to create arrays of any size. The fallback implementation makes use of Boost.Preprocessor to define N overloads taking 0...N arguments where N is set at compile time using the BOOST_MAKE_ARRAY_MAX_ARITY macro (with a default value of 10). The implementation has been compiled and tested with both g++-4.8 and clang++-3.0 in C++03 and C++11 modes. The code is available on github at https://github.com/kylelutz/make_array Any feedback is greatly appreciated. Thanks, Kyle

Greetings Kyle, On 01/12/2013 05:45 PM, Kyle Lutz wrote:
I'd like to propose a new function for the Boost Array library named make_array(). The function constructs a fixed size array given N arguments and is similar to the make_pair() and make_tuple() functions. [...] The code is available on github at https://github.com/kylelutz/make_array
Sorry if it's a genuine question... But in the case of C++11, wouldn't it be simpler to use an expansion instead of the "push_array" function? Something like this: #include <array> template<typename Head, typename... Args> struct get_first { typedef Head type; }; template<typename... Args> std::array<typename get_first<Args...>::type, sizeof...(Args)> make_array(Args&&... args) { return std::array<typename get_first<Args...>::type, sizeof...(Args)> { { args... } }; } Usage: auto my_array = make_array(1, 2, 3); static_assert(std::is_same<std::array<int, 3>, decltype(arr)>::value, "Error"); for(int v: arr) { std::cout << v << " "; } Tested with G++ 4.7.2, the "static_assert" does not fail. But maybe I'm missing something obvious... -- (o< | Yves Bailly | -o) //\ | Linux Dijon : http://www.coagul.org | //\ \_/ | | \_/`

On Jan 12, 2013, at 5:43 PM, Yves Bailly <yves.bailly@laposte.net> wrote:
Greetings Kyle,
On 01/12/2013 05:45 PM, Kyle Lutz wrote:
I'd like to propose a new function for the Boost Array library named make_array(). The function constructs a fixed size array given N arguments and is similar to the make_pair() and make_tuple() functions. [...] The code is available on github at https://github.com/kylelutz/make_array
Sorry if it's a genuine question... But in the case of C++11, wouldn't it be simpler to use an expansion instead of the "push_array" function? Something like this:
In C++11, you can just use an initializer list: boost::array<int, 5> {{ 0, 1, 2, 3, 4 }}; -- Marshall Marshall Clow Idio Software <mailto:mclow.lists@gmail.com> A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait). -- Yu Suzuki

On Jan 13, 2013, at 12:42 PM, Marshall Clow <mclow.lists@gmail.com> wrote:
On Jan 12, 2013, at 5:43 PM, Yves Bailly <yves.bailly@laposte.net> wrote:
Greetings Kyle,
On 01/12/2013 05:45 PM, Kyle Lutz wrote:
I'd like to propose a new function for the Boost Array library named make_array(). The function constructs a fixed size array given N arguments and is similar to the make_pair() and make_tuple() functions. [...] The code is available on github at https://github.com/kylelutz/make_array
Sorry if it's a genuine question... But in the case of C++11, wouldn't it be simpler to use an expansion instead of the "push_array" function? Something like this:
In C++11, you can just use an initializer list:
boost::array<int, 5> {{ 0, 1, 2, 3, 4 }};
Sorry. This works in both 03 and 11 (tried with gcc and clang): boost::array<int, 5> arr = { 0, 1, 2, 3, 4, }; (aggregate initialization) -- Marshall Marshall Clow Idio Software <mailto:mclow.lists@gmail.com> A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait). -- Yu Suzuki

On 13/01/2013 21:46, Marshall Clow wrote:
On Jan 13, 2013, at 12:42 PM, Marshall Clow <mclow.lists@gmail.com> wrote:
On Jan 12, 2013, at 5:43 PM, Yves Bailly <yves.bailly@laposte.net> wrote:
Greetings Kyle,
On 01/12/2013 05:45 PM, Kyle Lutz wrote:
I'd like to propose a new function for the Boost Array library named make_array(). The function constructs a fixed size array given N arguments and is similar to the make_pair() and make_tuple() functions. [...] The code is available on github at https://github.com/kylelutz/make_array
Sorry if it's a genuine question... But in the case of C++11, wouldn't it be simpler to use an expansion instead of the "push_array" function? Something like this:
In C++11, you can just use an initializer list:
boost::array<int, 5> {{ 0, 1, 2, 3, 4 }};
Sorry. This works in both 03 and 11 (tried with gcc and clang):
boost::array<int, 5> arr = { 0, 1, 2, 3, 4, };
(aggregate initialization)
The OP probably needs this for creating an array on-the-fly as an argument to a function.

On 01/13/2013 09:42 PM, Marshall Clow wrote:
On Jan 12, 2013, at 5:43 PM, Yves Bailly <yves.bailly@laposte.net> wrote:
On 01/12/2013 05:45 PM, Kyle Lutz wrote:
I'd like to propose a new function for the Boost Array library named make_array(). The function constructs a fixed size array given N arguments and is similar to the make_pair() and make_tuple() functions. [...] The code is available on github at https://github.com/kylelutz/make_array
Sorry if it's a genuine question... But in the case of C++11, wouldn't it be simpler to use an expansion instead of the "push_array" function? Something like this:
In C++11, you can just use an initializer list:
boost::array<int, 5> {{ 0, 1, 2, 3, 4 }};
I'm aware of this, I was just suggesting a simpler writing for the proposed "make_array" function. However I'm wondering about a use case where "make_array" would be useful, given the aggregate syntax. -- (o< | Yves Bailly | -o) //\ | Linux Dijon : http://www.coagul.org | //\ \_/ | | \_/`

On Sun, 13 Jan 2013, Yves Bailly wrote:
On 01/13/2013 09:42 PM, Marshall Clow wrote:
On Jan 12, 2013, at 5:43 PM, Yves Bailly <yves.bailly@laposte.net> wrote:
On 01/12/2013 05:45 PM, Kyle Lutz wrote:
I'd like to propose a new function for the Boost Array library named make_array(). The function constructs a fixed size array given N arguments and is similar to the make_pair() and make_tuple() functions. [...] The code is available on github at https://github.com/kylelutz/make_array
Sorry if it's a genuine question... But in the case of C++11, wouldn't it be simpler to use an expansion instead of the "push_array" function? Something like this:
In C++11, you can just use an initializer list:
boost::array<int, 5> {{ 0, 1, 2, 3, 4 }};
I'm aware of this, I was just suggesting a simpler writing for the proposed "make_array" function.
However I'm wondering about a use case where "make_array" would be useful, given the aggregate syntax.
People often complain that they can write: int a[]={1,2,3}; but for std::array they have to specify the size manually. make_array helps with that, for instance. Note that in some cases, we would want to be able to specify the type without specifying the length, it would be really cool if make_array<type>(a, b, c) also worked (using the same name makes it a bit complicated though). -- Marc Glisse

On Tue, 15 Jan 2013, Mathias Gaunard wrote:
On 13/01/2013 23:41, Marc Glisse wrote:
it would be really cool if make_array<type>(a, b, c) also worked (using the same name makes it a bit complicated though).
I don't see how using the same name brings any problem.
I didn't say it was impossible, just a bit more complicated, since you need to introduce this type as a template argument with a dummy default value. By the way, a link to Daniel Krügler's implementation of make_array: https://groups.google.com/d/msg/comp.lang.c++.moderated/8aWhRRsAO-w/qOvWAYJS... -- Marc Glisse

On Tue, Jan 15, 2013 at 4:06 PM, Marc Glisse <marc.glisse@inria.fr> wrote:
By the way, a link to Daniel Krügler's implementation of make_array:
https://groups.google.com/d/**msg/comp.lang.c++.moderated/** 8aWhRRsAO-w/qOvWAYJSaTUJ<https://groups.google.com/d/msg/comp.lang.c++.moderated/8aWhRRsAO-w/qOvWAYJSaTUJ>
I'd find make_array handy. I would also like a way to generate an array for constructing constant look-up tables. For example: https://github.com/joshuanapoli/table.

-----Original Message----- From: Boost [mailto:boost-bounces@lists.boost.org] On Behalf Of Joshua Napoli Sent: Wednesday, January 16, 2013 1:01 AM To: boost@lists.boost.org Subject: Re: [boost] Proposal: boost::make_array()
I'd find make_array handy. I would also like a way to generate an array for constructing constant look-up tables. For example: https://github.com/joshuanapoli/table.
I may like to use this within a Boost.Math /Multiprecision example. Please can it be Boost licensed (with you maintaining the copyright of course)? Thanks Paul --- Paul A. Bristow, Prizet Farmhouse, Kendal LA8 8AB UK +44 1539 561830 07714330204 pbristow@hetp.u-net.com

On Thu, Jan 17, 2013 at 4:55 AM, Paul A. Bristow <pbristow@hetp.u-net.com>wrote:
I may like to use this within a Boost.Math /Multiprecision example. Please can it be Boost licensed (with you maintaining the copyright of course)?
Sure thing. I just changed it to the boost license. I guess that you'll need a fallback for pre-C++11 compilers? Best, Josh

-----Original Message----- From: Boost [mailto:boost-bounces@lists.boost.org] On Behalf Of Joshua Napoli Sent: Thursday, January 17, 2013 7:35 PM To: boost@lists.boost.org Subject: Re: [boost] Proposal: boost::make_array()
On Thu, Jan 17, 2013 at 4:55 AM, Paul A. Bristow <pbristow@hetp.u-net.com>wrote:
I may like to use this within a Boost.Math /Multiprecision example. Please can it be Boost licensed (with you maintaining the copyright of course)?
Sure thing. I just changed it to the boost license. I guess that you'll need a fallback for
Thanks. pre-C++11
compilers?
Well I had a quick try with clang31 but seemed to lack std::array and changing to use boost::array didn't work either. In file included from i:/boost-sandbox/table_array/libs/test/table_test.cpp:16: In file included from i:/boost-sandbox/table_array\boost/table_array/table.hpp:19: i:/boost-sandbox/table_array\boost/table_array/detail/seq_table.hpp:27:5: error: expected member name or ';' after declaration specifiers {} ^ VS 2012 also refuses to jump for me! (failing at many places ). Looks neat but a bit bleeding edge ;-) Paul --- Paul A. Bristow, Prizet Farmhouse, Kendal LA8 8AB UK +44 1539 561830 07714330204 pbristow@hetp.u-net.com

On Saturday, January 12, 2013 11:45:01 Kyle Lutz wrote:
I'd like to propose a new function for the Boost Array library named make_array(). The function constructs a fixed size array given N arguments and is similar to the make_pair() and make_tuple() functions.
Interface:
The make_array() function provides the following interface:
template<class T> boost::array<T, N> make_array(...)
where the '...' indicates N arguments.
If no type T is passed explicitly it is deduced from the type of the first argument. The function requires that all of the arguments must be convertible (via static_cast<>) to type T. <snip>
The code is available on github at https://github.com/kylelutz/make_array
Any feedback is greatly appreciated.
The C++11 code looks too complicated. Wouldn't https://gist.github.com/4522812 be enough?
Thanks, Kyle
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

On 13/01/2013 10:38, Thomas Heller wrote:
The C++11 code looks too complicated. Wouldn't https://gist.github.com/4522812 be enough?
I don't think it is a good idea that make_array(1, 1., 1.f) makes an array of int. It seems quite arbitrary that the first argument would decide the type.

On Sunday, January 13, 2013 14:36:50 Mathias Gaunard wrote:
On 13/01/2013 10:38, Thomas Heller wrote:
The C++11 code looks too complicated. Wouldn't https://gist.github.com/4522812 be enough?
I don't think it is a good idea that make_array(1, 1., 1.f) makes an array of int. It seems quite arbitrary that the first argument would decide the type.
Right. That is another problem. I just tried to simplify the implementation of the original proposal.

On Sun, Jan 13, 2013 at 11:23 AM, Thomas Heller <thom.heller@gmail.com> wrote:
On Sunday, January 13, 2013 14:36:50 Mathias Gaunard wrote:
On 13/01/2013 10:38, Thomas Heller wrote:
The C++11 code looks too complicated. Wouldn't https://gist.github.com/4522812 be enough?
Indeed it is! I've updated the C++11 code to use the simpler initialization method. Thanks!
I don't think it is a good idea that make_array(1, 1., 1.f) makes an array of int. It seems quite arbitrary that the first argument would decide the type.
Right. That is another problem. I just tried to simplify the implementation of the original proposal.
As far as type-deduction, how would you guys feel about an implementation using boost::common_type? Something along the lines of: // make_array with deduced type template<class... Args> inline array<typename common_type<Args...>::type, sizeof...(Args)> make_array(Args&&... args) { typedef typename common_type<Args...>::type T; return array<T, sizeof...(Args)> {{ static_cast<T>(std::forward<Args>(args))... }}; } So that make_array(1, 2.1f) would return array<float, 2> and make_array(1, 2.1f, 3.2) would return array<double, 3>. Thoughts? Thanks, Kyle

On Sun, Jan 13, 2013 at 2:39 PM, Kyle Lutz <kyle.r.lutz@gmail.com> wrote:
On Sun, Jan 13, 2013 at 11:23 AM, Thomas Heller <thom.heller@gmail.com>
wrote:
On Sunday, January 13, 2013 14:36:50 Mathias Gaunard wrote:
On 13/01/2013 10:38, Thomas Heller wrote:
The C++11 code looks too complicated. Wouldn't https://gist.github.com/4522812 be enough?
Indeed it is! I've updated the C++11 code to use the simpler initialization method. Thanks!
I don't think it is a good idea that make_array(1, 1., 1.f) makes an array of int. It seems quite arbitrary that the first argument would decide the
type.
Right. That is another problem. I just tried to simplify the implementation of the original proposal.
As far as type-deduction, how would you guys feel about an implementation using boost::common_type? Something along the lines of:
// make_array with deduced type template<class... Args> inline array<typename common_type<Args...>::type, sizeof...(Args)> make_array(Args&&... args) { typedef typename common_type<Args...>::type T;
return array<T, sizeof...(Args)> {{ static_cast<T>(std::forward<Args>(args))... }}; }
So that make_array(1, 2.1f) would return array<float, 2> and make_array(1, 2.1f, 3.2) would return array<double, 3>.
Thoughts?
Thanks, Kyle
Thanks for the feedback everyone! I've updated the make_array() function to use boost::common_type<> to do the type-deduction instead of always using the type of the first argument. Furthermore, explicitly specifying the array's value type is still supported. To better illustrate, here's a set of make_array() invocations along with the return type: make_array(1) => array<int, 1> make_array<float>(1) => array<float, 1> make_array(1, 1.2f) => array<float, 2> make_array<int>(1, 1.2f) => array<int, 2> make_array(1, 1.2f, 3.2) => array<double, 3> Thoughts/comments? Cheers, Kyle

On Sat, Jan 12, 2013 at 11:45 AM, Kyle Lutz <kyle.r.lutz@gmail.com> wrote:
I'd like to propose a new function for the Boost Array library named make_array()...
Proposals for make_array have surfaced in the past, both on the Boost list and elsewhere. You might want to take a look at some these to gain insights. --Beman

On 15-01-2013 13:30, Beman Dawes wrote:
On Sat, Jan 12, 2013 at 11:45 AM, Kyle Lutz <kyle.r.lutz@gmail.com> wrote:
I'd like to propose a new function for the Boost Array library named make_array()...
Proposals for make_array have surfaced in the past, both on the Boost list and elsewhere. You might want to take a look at some these to gain insights.
You can also do boost::array<int,42> a = boost::assign::ref_list_of<3>( 0 )( 1 )( 2 ); -Thorsten

I am an algorithm developer. I also created in my own library such a function. I am using it for years, and found it very useful in everyday work. These very simple tools really help a lot. But according to my experience it is crucial that you keep it simple, something like: template <typename Item, typename Array = std::array<Item, n>> Array make_array (const Item & i_1, const Item & i_2, ..., const Item & i_n) { Array a {{i_1, ..., i_n }}; return a; } Separate implementations for each n. I am afraid that any complication (like boost::assign, boost::common_type) causes more practical trouble (longer compile time, cryptic compiler error messages) than gain.

-----Original Message----- From: Boost [mailto:boost-bounces@lists.boost.org] On Behalf Of Thorsten Ottosen Sent: Wednesday, January 16, 2013 8:23 AM To: boost@lists.boost.org Subject: Re: [boost] Proposal: boost::make_array()
On 15-01-2013 13:30, Beman Dawes wrote:
On Sat, Jan 12, 2013 at 11:45 AM, Kyle Lutz <kyle.r.lutz@gmail.com> wrote:
I'd like to propose a new function for the Boost Array library named make_array()...
Proposals for make_array have surfaced in the past, both on the Boost list and elsewhere. You might want to take a look at some these to gain insights.
You can also do
boost::array<int,42> a = boost::assign::ref_list_of<3>( 0 )( 1 )( 2 );
You can - but nearly everyone won't :-( Kyle's make_array looks *far* more appealing. It's unnecessary difficulty with trivial issues like array initialization that give C++ a bad name. (And of course if the idiotic decision not to make arrays first class citizens hadn't been inherited from C...) And Joshua's table way also meets another very common need. I think these two (together preferably) needs Boostifying and writing up with some examples, docs and tests, and review. Paul --- Paul A. Bristow, Prizet Farmhouse, Kendal LA8 8AB UK +44 1539 561830 07714330204 pbristow@hetp.u-net.com
participants (11)
-
Beman Dawes
-
Joshua Napoli
-
Kyle Lutz
-
Marc Glisse
-
Marshall Clow
-
Mathias Gaunard
-
Paul A. Bristow
-
Thomas Heller
-
Thorsten Ottosen
-
Yves Bailly
-
Zoltán Tóth