Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > partial specialization - why is this NOT ambiguous?

Reply
Thread Tools

partial specialization - why is this NOT ambiguous?

 
 
sks_cpp
Guest
Posts: n/a
 
      08-24-2003
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?


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      08-24-2003
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.

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-24-2003

"sks_cpp" <(E-Mail Removed)> wrote in message
news:dLT1b.71204$(E-Mail Removed).. .
> 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



 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      08-24-2003
John Harrison wrote:
> "sks_cpp" <(E-Mail Removed)> wrote in message
> news:dLT1b.71204$(E-Mail Removed).. .
>
>>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()
{
}



 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      08-24-2003

"John Harrison" <(E-Mail Removed)> wrote in message
news:bi9gl1$6s2j8$(E-Mail Removed)-berlin.de...
>
> "sks_cpp" <(E-Mail Removed)> wrote in message
> news:dLT1b.71204$(E-Mail Removed).. .
> > 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


 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      08-24-2003
Rob Williscroft wrote:
> Gianni Mariani wrote in news:bi961b$(E-Mail Removed):
>


>
> It was compilable.
>


how so ?

 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      08-25-2003
Gianni Mariani wrote in news:bibhr0$(E-Mail Removed):

> Rob Williscroft wrote:
>> Gianni Mariani wrote in news:bi961b$(E-Mail Removed):
>>

>
>>
>> It was compilable.
>>

>
> how so ?
>


#include <iostream>
#include <ostream>
#include <iomanip>

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

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


something<int, bool> a;


int main()
{
using namespace std;

cout << "It compiled" << endl;
}

Try it and see, I get:

It compiled

With 3 different compilers.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      08-25-2003
Rob Williscroft wrote:
> Gianni Mariani wrote in news:bibhr0$(E-Mail Removed):
>


> Try it and see, I get:
>
> It compiled
>
> With 3 different compilers.
>


That's nice, that's not what was posted :^)

 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      08-26-2003
On Sun, 24 Aug 2003 00:35:21 GMT, "sks_cpp" <(E-Mail Removed)>
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Template parameters not used in partial specialization when usingtypedefined jrwats C++ 2 07-11-2009 07:57 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why? Partial Class within a Partial class Billy ASP .Net 2 02-01-2006 09:10 AM
Partial but not full/explicit specialization at non-namespace scope. Ferdi Smit C++ 8 10-19-2005 10:27 PM
standard not clear about member function partial specialization SainTiss C++ 1 12-25-2003 08:47 PM



Advertisments