std array deduction guide prevents nested braces

47 views
Skip to first unread message

Johannes Schaub

unread,
Jun 16, 2017, 5:47:45 AM6/16/17
to std-dis...@isocpp.org
The deduction guide for std::array apparently is written as (see
http://en.cppreference.com/w/cpp/container/array/deduction_guides )

template <class T, class... U>
array(T, U...) -> array<T, 1 + sizeof...(U)>;

However this prevents using nested braces

array<int, 3> a;
array b{a, {1, 2, 3}, {4, 5, 6}};

The declaration of "b" is not possible, because while T is array<int,
3>, all elements of U cannot be deduced. I propose to write the
deduction guide as

template <class T, int N>
array(T(&&)[N]) -> array<T, N>;

This has multiple benefits: We can write the above - able to use
braces as long as some elements hint the deduction process about the
type. And the other benefit is that all elements are intrinsically
required to yield the same type, without needing a "is_same<T,
Us...>::type need to be true" extra rule. The first benefit however is
the one that's far more important.
Reply all
Reply to author
Forward
0 new messages