Re: std::array - if only it knew its size
Am 05.04.2011 23:22, schrieb Daniel Kr?gler:
Am 05.04.2011 18:52, schrieb Ricky65:
I am pleased that C++0x includes a container for fixed-size arrays -
std::array. The obvious advantages being that, unlike built in arrays,
it's easy to get the size of the array using the size() method and the
array doesn't "decay" to a pointer like a built in array and so on. It
goes without saying that these problems with built in arrays cause a
lot of bugs. However, I feel there is a significant shortcoming with
this container - std::array cannot deduce the size of the array from
the initializer list. From what I understand, this is the only
advantage built in arrays have over std::array. This limitation can be
a problem when initializing an array with lots of items.
[..]
I think what you want is something like make_array as suggested in
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#851
This issues has been deferred as NAD future, because there was no
pressing need to provide this for C++0x.
Until that point you can simply define your own make_array as described
in the issue:
#include <type_traits>
#include <utility>
#include <array>
template <typename... T>
std::array<typename std::decay<typename std::common_type<T...>::type
::type, sizeof...(T)>
make_array(T&&... t)
{
typedef typename std::decay<typename std::common_type<T...>::type
>::type U;
std::array<U, sizeof...(T)> result = {
static_cast<U>(std::forward<T>(t))...
};
return result;
}
int main()
{
auto a = make_array(1, 2); // OK: std::array<int, 2>
auto b = make_array(1, 1.2, 2u); // OK: std::array<double, 3>
}
One reason for not standardizing too early things is that you can
improve your original ideas. Also, since then some core rules have
changed in advantage of the programmer. Since the original proposal done
in LWG 851 I had developed make_array a bit further and here is my
currently best approximation: The advantages are:
1) make_array can now be used in constant expressions
2) Users can provide there own destination type
3) Support for empty arrays
4) Validation that all arguments are implicitly convertible to the
destination type (Remove this rule, if you don't like it, or replace it
by a corresponding is_constructible test or sfinae the template away).
#include <type_traits>
#include <utility>
#include <array>
// Assume that std::forward were required to be constexpr...
template <class T>
constexpr T&&
cforward(typename std::remove_reference<T>::type& t) {
return static_cast<T&&>(t);
}
template <class T>
constexpr T&&
cforward(typename std::remove_reference<T>::type&& t) {
static_assert(!std::is_lvalue_reference<T>::value,
"T must not be an lvalue-reference");
return static_cast<T&&>(t);
}
template<class T, class... U>
struct all_convertible_to;
template<class T>
struct all_convertible_to<T> : std::true_type {};
template<class T, class U>
struct all_convertible_to<T, U> : std::is_convertible<U, T> {};
template<class T, class U, class... R>
struct all_convertible_to<T, U, R...> : std::conditional<
std::is_convertible<U, T>::value, all_convertible_to<T, R...>,
std::false_type>::type::type
{};
struct default_array_type_t;
template<class T, class...>
struct deduce_array_type
{
typedef T type;
};
template<class... T>
struct deduce_array_type<default_array_type_t, T...>
{
typedef typename std::decay<
typename std::common_type<T...>::type
>::type type;
};
// Sfinae protection for the empty sequence case: Use code is
// required to provide the destination type
template<>
struct deduce_array_type<default_array_type_t>
{
};
template <class D = default_array_type_t, class... T>
constexpr auto make_array(T&&... t) ->
std::array<typename deduce_array_type<D, T...>::type, sizeof...(T)>
{
typedef typename deduce_array_type<D, T...>::type U;
static_assert(all_convertible_to<U, T...>::value,
"Argument types must be implicitly convertible to destination type");
return std::array<U, sizeof...(T)>{
static_cast<U>(cforward<T>(t))...
};
}
int main()
{
auto a = make_array(1, 2);
auto b = make_array(5, 1.2);
auto c = make_array<float>(-12, 7.3);
auto d = make_array<bool>();
constexpr auto ca = make_array(-1, +1, -2, +2);
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]