According to [temp.class.order] §14.5.5.2, the selection of a partial specialization of t
in this example:
template< typename >
struct s { typedef void v, w; };
template< typename, typename = void >
struct t {};
template< typename c >
struct t< c, typename c::v > {};
template< typename c >
struct t< s< c >, typename s< c >::w > {};
t< s< int > > q;
is equivalent to the selection of an overload of f
in this example:
template< typename >
struct s { typedef void v, w; };
template< typename, typename = void >
struct t {};
template< typename c >
constexpr int f( t< c, typename c::v > ) { return 1; }
template< typename c >
constexpr int f( t< s< c >, typename s< c >::w > ) { return 2; }
static_assert ( f( t< s< int > >() ) == 2, "" );
However, GCC, Clang, and ICC all reject the first example as ambiguous, but accept the second.
Even more strangely, the first example works if ::v
is replaced with ::w
or vice versa. The non-deduced contexts c::
and s< c >::
are apparently being considered in specialization ordering, which doesn't make sense.
Am I missing something in the standard, or do all these implementations have the same bug?
I feel the intent is that the examples compile, however, the standard doesn't clearly state what should happen (if anything) when matching of template argument lists for the synthesized argument lists used by partial ordering (14.5.5.1/1):
The above paragraph is required so that #1 is selected in the following:
Here:
T
is deduced toint
(14.5.5.1/2)int
==int
,void
==void
(14.5.5.1/1)For the partial ordering case:
For the first parameter, #4 is more specialized and both second parameters are non-deduced contexts, ie. type deduction succeeds for #4 to #3 but not for #3 to #4.
I think the compilers are then appling the "argument lists must match" rule from 14.5.5.1/1 on the synthesized argument lists. This compares the first synthesized type
Q1::v
to the seconds<Q2>::w
and these types are not the same.This may explain why changing
v
tow
resulted in some examples working, as the compiler decided those types were the same.This is not an issue outside of partial ordering, because the types are concrete as types like
c::v
will be instantiated tovoid
etc.It could be that the committee intends for the type equivalence (14.4) to apply, but I don't think so. The standard should probably just clarify exactly what should happen regarding matching (or not) of synthesized types created as part of the partial ordering step.
Switching to extremely-pedantic-mode for a moment, yes, I think you are missing something in the standard, and no, it shouldn't make any difference in this case.
All standard references are to N4527, the current working draft.
[14.5.5.2p1] says:
Going to [14.5.6.2p1]:
No mention of partial ordering of class template specializations. However, [14.8.2.4p3] says:
Even though it refers back to [14.5.6.2], it does say "other contexts". I can only conclude that, when applying the partial ordering algorithm to the function templates generated according to the rules in [14.5.5.2], the function template’s function type is used, not the list of parameter types, as it would happen for a function call.
So, the selection of a partial specialization of
t
in your first snippet would be equivalent not to a case involving a function call, but to one that takes the address of a function template (for example), which also falls under "other contexts":(Since we're still in extremely-pedantic-mode, I rewrote the function templates exactly like the example in [14.5.5.2p2].)
Needless to say, this also compiles and prints
t<s<C>, s<C>::w>
. The chances of it producing different behaviour were slim, but I had to try it. Considering how the algorithm works, it would have made a difference if the function parameters were, for example, reference types (triggering the special rules in [14.8.2.4] in the case of a function call, but not in the other cases), but such forms can't occur with function templates generated from class template specializations.So, this whole detour didn't help us one bit, but... it's a
language-lawyer
question, we had to have some standard quotes in here...There are some active Core issues related to your example:
1157 contains a note that I think is relevant:
I'm not entirely sure that's so clearly specified; after all, [14.8.2.5p1] says
and [14.8.2.4] references [14.8.2.5] in its entirety. However, it's pretty clear that partial ordering of function templates doesn't look for compatibility when non-deduced contexts are involved, and changing that would break a lot of valid cases, so I think this is just a lack of proper specification in the standard.
To a lesser extent, 1847 has to do with non-deduced contexts appearing in arguments to template specializations. It references 1391 for the resolution; I think there are some issues with that wording - more details in this answer.
To me, all of this says that your example should work.
Like you, I was quite intrigued by the fact that the same inconsistency is present in three different compilers. I got even more intrigued after I verified that MSVC 14 exhibits the exact same behaviour as the others. So, when I got some time, I thought I'd take a quick look at what Clang does; it turned out to be anything but quick, but it yielded some answers.
All the code relevant to our case is in
lib/Sema/SemaTemplateDeduction.cpp
.The core of the deduction algorithm is the
DeduceTemplateArgumentsByTypeMatch
function; all variants of deduction end up calling it, and it's then used recursively to walk through the structure of compound types, sometimes with the help of the heavily overloadedDeduceTemplateArguments
set of functions, and some flags to adjust the algorithm based on the specific type of deduction being done and the parts of a type's form being looked at.An important aspect to note regarding this function is that it handles strictly deduction, and not substitution. It compares type forms, deduces template argument values for template parameters that appear in deduced contexts, and skips non-deduced contexts. The only other check it does is verifying that deduced argument values for a template parameter are consistent. I've written some more about the way Clang does deduction during partial ordering in the answer I mentioned above.
For partial ordering of function templates, the algorithm starts in the
Sema::getMoreSpecializedTemplate
member function, which uses a flag of typeenum TPOC
to determine the context for which partial ordering is being done; the enumerators areTPOC_Call
,TPOC_Conversion
, andTPOC_Other
; self-explanatory. This function then callsisAtLeastAsSpecializedAs
twice, back and forth between the two templates, and compares the results.isAtLeastAsSpecializedAs
switches on the value of theTPOC
flag, makes some adjustments based on that, and ends up calling, directly or indirectly,DeduceTemplateArgumentsByTypeMatch
. If that returnsSema::TDK_Success
,isAtLeastAsSpecializedAs
does only one more check, to verify that all template parameters that are used for partial ordering have values. If that's good too, it returnstrue
.And that's partial ordering for function templates. Based on the paragraphs quoted in the previous section, I was expecting partial ordering for class template specializations to call
Sema::getMoreSpecializedTemplate
with suitably constructed function templates and a flag ofTPOC_Other
, and everything to flow naturally from there. If that were the case, your example should work. Surprise: that's not what happens.Partial ordering for class template specializations starts in
Sema::getMoreSpecializedPartialSpecialization
. As an optimization (red flag!), it doesn't synthesize function templates, but rather usesDeduceTemplateArgumentsByTypeMatch
to do type deduction directly on the class template specializations themselves as the types ofP
andA
. This is fine; after all, that's what the algorithm for function templates would end up doing anyway.However, if all goes well during deduction, it then calls
FinishTemplateArgumentDeduction
(the overload for class template specializations), which does substitution and other checks, including checking that the substituted arguments to the specialization are equivalent to the original ones. This would be fine if the code were checking whether a partial specialization matches a set of arguments, but is not fine during partial ordering, and, as far as I can tell, causes the problem with your example.So, it seems that Richard Corden's assumption as to what happens is correct, but I'm not entirely sure this was intentional. This looks more like an oversight to me. How we ended up with all compilers behaving the same way remains a mystery.
In my opinion, removing the two calls to
FinishTemplateArgumentDeduction
fromSema::getMoreSpecializedPartialSpecialization
would do no harm, and would restore consistency to the partial ordering algorithm. There's no need for the additional check (done byisAtLeastAsSpecializedAs
) that all template parameters have values either, as we know all template parameters are deducible from the specialization's arguments; if they weren't, the partial specialization would fail matching, so we wouldn't get to partial ordering in the first place. (Whether such partial specializations are allowed in the first place is the subject of issue 549. Clang issues a warning for such cases, MSVC and GCC issue an error. Anyway, not a problem.)As a side note, I think all of this applies to the overload for variable template specializations as well.
Unfortunately, I don't have a build environment set up for Clang, so I can't test this change at the moment.
The information in this answer is based in large part off of this question. The template partial ordering algorithm is underspecified by the standard. The main compilers seem to at least agree on what the algorithm should be though.
To start with, your two examples aren't equivalent. You have two template specializations in addition to your primary template, but with your function example, you're not adding a function overload for the primary. If you add it:
The function call becomes ambiguous as well. The reason for this is that partial ordering type synthesis algorithm does not instantiate templates and instead synthesizes new unique types.
First, if we compare the function I just introduced to this one:
We have:
We ignore non-deduced contexts in the partial ordering deduction rules, so
Unique0
matchesc
, butUnique1_v
does not matchvoid
! Thus0
is preferred. This is probably not what you expected.If we then compare
0
and2
:Here, the
0
deduction fails (sinceUnique0
won't matchs<c>
), but the2
deduction also fails (sinceUnique2_v
won't matchvoid
). That's why it's ambiguous.This lead me to an interesting question about
void_t
:This function overload:
would be preferred over
0
, since the arguments would bes<c>
andvoid
. But this one would not be:Since we wouldn't instantiate
make_void<s<c>>
in order to determine::type
, so we end up in the same situation as2
.