Re: ambiguous overload?
highegg wrote:
On 15 Pro, 14:48, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
highegg wrote:
On 12 Pro, 19:42, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
Victor Bazarov wrote:
highegg wrote:
given the declarations
class A {};
template <class X, class T> void method (double u, T v);
template <class X> void method (double u, long v);
...
double u; long v;
Is the following call unambigous (w.r.t. C++ standard)?
method<A> (u, v);
[..]
Yes.
I meant to say it was ambiguous. If you know what part of the Draft
says the second is a better match, could you point to it, please?
From what I figure, the first template could be more specialised than
the second because it has two types defined, not just one. However, all
the examples given in the Standard have to do with A<T*> vs A<T> (which
makes the former more specialised) and not A<T,U> vs A<T>. My
understanding of what makes templates "more specialised" can be
incorrect, so I'd appreciated somebody's explanation.
Hi Victor,
after reading the relevant sections thoroughly, I think it is the
partial template ordering rules in 14.5.5.2 that account for the
unambigous resolution. See the paragraphs 2-5.
Simply said, the reasoning is that the second template's prototype can
be matched by the first, but not vice versa (note the wording in
paragraph 3 saying that a *unique* type is synthesized for each type
template parameter, i.e. for T in this case).
Therefore, the second template is more specialized and will be
preferred.
If anyone thinks I'm wrong with this reasoning, please clarify.
Anyway, thanks to everyone who replied, especially Joe pointing me to
Comeau C++.
Jaroslav Hajek
Thanks, Jaroslav. I take it you meant 14.5.6.2 (not .5.2). I can't
find that reasoning you talk about (yes, I have read paragraphs 2-5 in
the [temp.func.order] section). Can you please lay your deductions down
so that my feeble brain can understand them? Apparently, somewhere in
the paragraph 3's first sentence I get lost. How does that sentence
explain that in your example the second template is more specialised?
Much appreciated!
Sorry. I was referring to the 1998 standard.
Don't apologise, I was looking in the latest draft. My fault.
> Anyway, I'm attaching the
relevant text:
2. Given two overloaded function templates, whether one is more
specialized than another can be determined by transforming each
template in turn and using argument deduction (14.8.2) to compare it
to the other.
3. The transformation used is:
-- For each type template parameter, synthesize a unique type and
substitute that for each occurrence of that parameter in the function
parameter list, or for a template conversion function, in the return
type.
-- For each non-type template parameter... (not relevant)
-- For each template template parameter...
4. Using the transformed function parameter list, perform argument
deduction against the other function template. The transformed
template is at least as specialized as the other if, and only if, the
deduction succeeds and the deduced parameter types are an exact match
(so the deduction does not rely on implicit conversions).
5. A template is more specialized than another if, and only if, it is
at least as specialized as the other template and that template is not
at least as specialized as the first.
So, now we're given our pair of declarations
template <class X, class T> void method (double u, T v);
template <class X> void method (double u, long v);
Let's check whether the first is more specialized than the second.
For each type template parameter, we synthesize a *unique* type, i.e.
we declare:
class XX {}; class TT {};
and try matching the prototype
method<XX> (double, TT);
against the second template - it fails.
That's where I don't see it, I guess. You're saying "it fails" because
the second one cannot be used since TT has no conversion to 'long', is
that it?
Now do it the other way around, and match
method<XX> (double, long)
against the first template - it succeeds and it is an exact match (T =
long).
OK, so the deduction succeeds means that the second is at least as
specialised as the first. And since before the first failed, it can't
be as specialised as the second, that means the second is more
specialised. Did I get that right?
Therefore, by paragraph 4, the second template is at least as
specialized as the first, and because the converse does not hold, the
inequality is strict by paragraph 5.
At least, this is my understanding. Comments are welcome.
Sounds reasonable. Thank you again for the explanation!
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask