[Bug libstdc++/53248] std::array<T,0> doesn't work when T is not default-constructible
daniel.kruegler at googlemail dot com
gcc-bugzilla@gcc.gnu.org
Sun May 6 11:41:00 GMT 2012
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53248
--- Comment #3 from Daniel Krügler <daniel.kruegler at googlemail dot com> 2012-05-06 11:21:47 UTC ---
(In reply to comment #2)
> Daniel, can you see other options besides adding a specialization? (which would
> be a straightforward task, I may even get around to do pretty soon when I will
> do debug-mode std::array, already in my todo list)
I haven't implemented it completely, but an alternative to a partial
specialization could be something like the following:
template <class T, size_t N>
struct __array_data_traits
{
typedef T type[N];
};
template <class T>
struct __array_data_traits<T, 0>
{
struct type {};
};
template <class T, size_t N>
struct array
{
typename __array_data_traits<T, N>::type elems;
};
array<int, 1> ai1 = {1};
array<int, 1> ai1b = {};
array<int, 0> ai0 = {};
The sole (?) disadvantage of this approach would be that
std::is_empty<array<int, 0>> does not evaluate to true. But all the
specialization-relevant logic could also be added to __array_data_traits in
terms of static (constexpr where possible) functions.
Technically the traits-based approach would be preferable, because otherwise I
see the risk that user-code that itself would try to specialize std::array with
some T for a user-provided type could notice the difference. Consider:
// Within Library:
template <class T, size_t N>
struct array
{
T elems[N];
};
template <class T>
struct array<T, 0>
{
};
// User world:
struct User {};
template <size_t N>
struct array<User, N>
{
User elems[N > 0 ? N : 1];
};
array<User, 0> au0 = {}; // Error, ambiguous
The problem with the current library wording is that it does not *explicitly*
allow a *specialization* for std::array (in contrast to vector<bool> or to
numeric_limits for cv-variations of the actual type), therefore I believe that
from the current wording state a partial-specialization by the library is
borderline to invalid, at least a gray zone, because of the need to support
[namespace.std] p1.
If a library issue should be submitted, I would suggest to provide the library
the freedom for providing a special for the zero-size case.
More information about the Gcc-bugs
mailing list