Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we name an ill-formed specialization as long as we don't instantiate it?

template <typename T, int N>
struct array {
    T data[N];
};

using bad_array = array<void, 0>;

In the above code, we are referring to a specialization of the array class template which is not instantiated. If it was instantiated, then the program would be ill-formed, because we would generate a void data[0] member.

Is this code ill-formed, no diagnostic required, or is it well-formed?

like image 843
Jan Schultke Avatar asked Nov 01 '25 16:11

Jan Schultke


1 Answers

Yes, it is well-formed. I can't really provide you a standard reference, because it simply doesn't violate anything in it.

Nothing says that forming a type alias requires the type to be complete, nor does completeness of the type affect the behavior of the alias declaration and so there is no implicit instantiation according to [temp.inst]/2. So, that the instantiation of the particular specialization would be ill-formed is irrelevant until you instantiate it implicitly or explicit in some other way.

And since also none of the items listed in [temp.res.general]/8 apply to your template, the compiler shall not issue any diagnostics for the template itself as required by the last sentence of [temp.res.general]/8.

When writing a template-id of anything but a function template, then regardless of context, the template-id must be valid, otherwise the program is ill-formed. The requirements for this are in [temp.names]/6 and include verifying that the number and kinds of template arguments are correct as well as that constraints are satisfied for non-dependent template-ids, i.e. all the syntactical requirements connected to the declaration of the template rather than its definition. But none of these requirements are violated by the template-id array<void, 0> either.

like image 87
user17732522 Avatar answered Nov 03 '25 09:11

user17732522