|
Boost : |
Subject: [boost] Proposal: boost::make_array()
From: Kyle Lutz (kyle.r.lutz_at_[hidden])
Date: 2013-01-12 11:45:01
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
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk