In 14.8.2.4p10 of the C++11 draft, there is written
If for each type being considered a given template is at least as specialized for all types and more specialized for some set of types and the other template is not more specialized for any types or is not at least as specialized for any types, then the given template is more specialized than the other template.
Why is there a "or is not at least as specialized for any types"? As far as I can see, if we have a list of types
T1, T2, T3
U1, U2, U3
And if all Ts are at least as specialized and some are more specialized. And none of the Us are more specialized, then it seems to me that it follows that the set of T as a whole is more specialized than the set of U, logically speaking. Why is there then that mentioned fallback for when none of the Us are at least as specialized than the corresponding Ts?
Update: This has now been added as an official C++ Issue
I have finally figured out how to read the paragraph in question. Below I have bulleted it
This way the following first template is also more specialized than the second template
Note that the first template's parameter is at least as specialized as the second template, but is not defined to be "more specialized" - that term only applies for the case where both parameters were references and certain conditions apply (see paragraph 9 of 14.8.2.4) . The rules are apparently not meant to follow any formal ordering laws. The second template is not at least as specialized as the first template. This means that the second bullet applies, and not the first.
This answer is based on an incorrect parsing of the Standard paragraph's abstract syntax tree. The grouping of conditions assumed in the "Back to the Standard" section turned out not to be the intended one. The intended grouping is the one Johannes Schaub has shown in his answer.
I agree with you that the second part (actually, the whole second condition) is redundant.
Some vocabulary of reference:
Let's have some fun with logics and introduce 3 fundamental relations between two templates for a pair of corresponding parameters:
Ti
andUi
respectively, one template matches the other but not vice versa. I will indicate this asTi < Ui
;Ti
andUi
respectively, one template matches the other and vice versa. I will indicate this asTi == Ui
;Ti
andUi
respectively, none of the templates matches the other for the particular parameter. I will indicate this asT1 ~ U1
.For instance, in the code snippet below:
For the first parameter, (1) is more specialized than (
<
) (2); for the second parameter, (1) is equally specialized as (or "as specialized as",==
) (2); for the third parameter, (1) is specialization-incomparable to (~
) (2).And let' now define a derived relation:
Ti
andUi
when(Ti < Ui)
or(Ti == Ui)
, i.e. when either (1) is more specialized than (2) or (1) is as specialized as (2). In the above example, therefore,T1 <= U1
,T2 <= U2
, andU2 <= T2
.Back to the Standard:
With the help of a couple of parentheses, the quote above becomes (A && (B1 || B2)):
Given two templates to be ordered with respect to the corresponding sequences of parameter types
T1, ..., Tn
andU1, ..., Un
, the condition (A):Means that for each
i = 1..n
,Ti <= Ui
, and for somej
s in1..n
, it applies the stricter condition thatTj < Uj
. Dropping the indexi
, this means that for each parameter:This condition is put in logical conjunction ("and") with another condition (B), which is in turn the logical disjunction ("or") of two sub-conditions, (B1) and (B2). Let's start examining sub-condition (B1):
This means that for any
i
, it is never the case thatUi < Ti
, which means that either:Ti
is more specialized thanUi
(Ti < Ui
); orTi
andUi
are equally specialized (Ui == Ti
); orTi
andUi
are specialization-incomparable (Ui ~ Ti
):More formally:
Now let's see the second sub-condition (B2), which is put in logical disjunction with (B1):
This is the negation of
U <= T
, which means:So in other words,
T
andU
are not equally-specialized, norU
is more specialized thanT
. Therefore, the only possibilities left are that:Now it is evident that (B2) implies (B1), because (B2) is more restrictive. Therefore, their disjunct (B) will be coincident with (B1), and (B2) is redundant:
But what is also evident here is that (A) is stricter than (B), so the conjunction of (A) and (B) is equivalent to (A).
Conclusion:
The whole condition (B) is redundant.