partial specialization - why is this NOT ambiguous?

Discussion in 'C++' started by sks_cpp, Aug 24, 2003.

  1. sks_cpp

    sks_cpp Guest

    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?
     
    sks_cpp, Aug 24, 2003
    #1
    1. Advertising

  2. 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.
     
    Gianni Mariani, Aug 24, 2003
    #2
    1. Advertising

  3. "sks_cpp" <> wrote in message
    news:dLT1b.71204$...
    > 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
     
    John Harrison, Aug 24, 2003
    #3
  4. John Harrison wrote:
    > "sks_cpp" <> wrote in message
    > news:dLT1b.71204$...
    >
    >>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()
    {
    }
     
    Gianni Mariani, Aug 24, 2003
    #4
  5. "John Harrison" <> wrote in message
    news:bi9gl1$6s2j8$-berlin.de...
    >
    > "sks_cpp" <> wrote in message
    > news:dLT1b.71204$...
    > > 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
     
    John Harrison, Aug 24, 2003
    #5
  6. Rob Williscroft wrote:
    > Gianni Mariani wrote in news:bi961b$:
    >


    >
    > It was compilable.
    >


    how so ?
     
    Gianni Mariani, Aug 25, 2003
    #6
  7. Gianni Mariani wrote in news:bibhr0$:

    > Rob Williscroft wrote:
    >> Gianni Mariani wrote in news:bi961b$:
    >>

    >
    >>
    >> 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/
     
    Rob Williscroft, Aug 25, 2003
    #7
  8. Rob Williscroft wrote:
    > Gianni Mariani wrote in news:bibhr0$:
    >


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


    That's nice, that's not what was posted :^)
     
    Gianni Mariani, Aug 25, 2003
    #8
  9. sks_cpp

    tom_usenet Guest

    On Sun, 24 Aug 2003 00:35:21 GMT, "sks_cpp" <>
    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
     
    tom_usenet, Aug 26, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Billy
    Replies:
    2
    Views:
    528
    Billy
    Feb 1, 2006
  2. SainTiss
    Replies:
    1
    Views:
    418
    SainTiss
    Dec 25, 2003
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,216
    Smokey Grindel
    Dec 2, 2006
  4. Ferdi Smit
    Replies:
    8
    Views:
    2,570
  5. jrwats
    Replies:
    2
    Views:
    1,640
    jrwats
    Jul 11, 2009
Loading...

Share This Page