By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

partial specialization - why is this NOT ambiguous?

P: n/a
template< class a, class b, class c = int >
struct something
{
};

template< class a, class b >
struct something<a, b>
{
};
Why is the following line NOT ambiguous (according to gcc)?

something<int, bool> a;

Shouldn't that be ambiguous?
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
sks_cpp wrote:
....

Shouldn't that be ambiguous?


#include <iostream>

template< class a, class b, class c = int >
struct something
{
something() { std::cout << "A\n"; }
};

template< class a, class b >
struct something<a, b>
{
something() { std::cout << "B\n"; }
};
something<int, bool, int> a;
something<int, bool> b;

int main()
{
}

.... why does this print

B
B

?

BTW - next time post compilable code.

I would guess this is an error in GCC but I still don't grok all the
fine points of templates so I may be wrong.

Jul 19 '05 #2

P: n/a

"sks_cpp" <sk*****@hotmail.com> wrote in message
news:dL*******************@news2.central.cox.net.. .
template< class a, class b, class c = int >
struct something
{
};

template< class a, class b >
struct something<a, b>
{
};
Why is the following line NOT ambiguous (according to gcc)?

something<int, bool> a;

Shouldn't that be ambiguous?


I don't think so. The second template looks more specialised than the first
to me.

Ambiguity is not the issue. The compiler picks the most specialised
template, the second is more specialised because (for instance)
something<int, int, int> will match the first not the second, but there is
no substitutions that will match the second and not the first. Therefore the
second is more specialised.

john

Jul 19 '05 #3

P: n/a
John Harrison wrote:
"sks_cpp" <sk*****@hotmail.com> wrote in message
news:dL*******************@news2.central.cox.net.. .
template< class a, class b, class c = int >
struct something
{
};

template< class a, class b >
struct something<a, b>
{
};
Why is the following line NOT ambiguous (according to gcc)?

something<int, bool> a;

Shouldn't that be ambiguous?

I don't think so. The second template looks more specialised than the first
to me.

Ambiguity is not the issue. The compiler picks the most specialised
template, the second is more specialised because (for instance)
something<int, int, int> will match the first not the second, but there is
no substitutions that will match the second and not the first. Therefore the
second is more specialised.


so what *should* this print.

#include <iostream>

template< class a, class b, class c = int >
struct something
{
something() { std::cout << "A\n"; }
};

template< class a, class b >
struct something<a, b>
{
something() { std::cout << "B\n"; }
};
something<int, bool, int> a;
something<int, bool> b;

int main()
{
}

Jul 19 '05 #4

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bi************@ID-196037.news.uni-berlin.de...

"sks_cpp" <sk*****@hotmail.com> wrote in message
news:dL*******************@news2.central.cox.net.. .
template< class a, class b, class c = int >
struct something
{
};

template< class a, class b >
struct something<a, b>
{
};
Why is the following line NOT ambiguous (according to gcc)?

something<int, bool> a;

Shouldn't that be ambiguous?


Forget my first post, I was getting confused between your case and the
choice between alternate partial template specialisations. Since you only
have one partial template specialisation, it will be used if it is matched.

Your example is clever because it confuses two different processes. My take
on it is this. By saying

template< class a, class b, class c = int > something ...

in the primary template you are saying that whenever the template something
appears with two arguments a third int argument is added. I.e.

something<int, int> a; is equivalent to something <int, int, int> a;

and also that

template< class a, class b >
struct something<a, b>
{
};

is equivalent to

template< class a, class b >
struct something<a, b, int>
{
};

Once we see that, it obvious whether the specialisation applies or not. If
the third template argument is an int (explicitly or implicitly), use the
specialisation. I.e.

something<int, int> a; // implicit int, use specialisation
something<int, int, int> b; // explicit int, use specialisation
something<int, int, double> c; // not an int, use primary

john
Jul 19 '05 #5

P: n/a
On Sun, 24 Aug 2003 00:35:21 GMT, "sks_cpp" <sk*****@hotmail.com>
wrote:
template< class a, class b, class c = int >
struct something
{
};

template< class a, class b >
struct something<a, b>
{
};
Why is the following line NOT ambiguous (according to gcc)?

something<int, bool> a;

Shouldn't that be ambiguous?


No, you have the partial specialization:

template <class a, class b>
struct something<a, b, int>

which obviously matches something<int, bool, int>. Default parameters
are syntatic sugar, they don't effect things like specialization.

Tom
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.