partial specialization - why is this NOT ambiguous?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sks_cpp

    partial specialization - why is this NOT ambiguous?

    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?


  • Gianni Mariani

    #2
    Re: partial specialization - why is this NOT ambiguous?

    sks_cpp wrote:
    ....[color=blue]
    >
    > Shouldn't that be ambiguous?
    >[/color]

    #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.

    Comment

    • John Harrison

      #3
      Re: partial specialization - why is this NOT ambiguous?


      "sks_cpp" <sksjava@hotmai l.com> wrote in message
      news:dLT1b.7120 4$Ij4.37013@new s2.central.cox. net...[color=blue]
      > 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?
      >[/color]

      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



      Comment

      • Gianni Mariani

        #4
        Re: partial specialization - why is this NOT ambiguous?

        John Harrison wrote:[color=blue]
        > "sks_cpp" <sksjava@hotmai l.com> wrote in message
        > news:dLT1b.7120 4$Ij4.37013@new s2.central.cox. net...
        >[color=green]
        >>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?
        >>[/color]
        >
        >
        > 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.
        >[/color]

        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()
        {
        }



        Comment

        • John Harrison

          #5
          Re: partial specialization - why is this NOT ambiguous?


          "John Harrison" <john_andronicu s@hotmail.com> wrote in message
          news:bi9gl1$6s2 j8$1@ID-196037.news.uni-berlin.de...[color=blue]
          >
          > "sks_cpp" <sksjava@hotmai l.com> wrote in message
          > news:dLT1b.7120 4$Ij4.37013@new s2.central.cox. net...[color=green]
          > > 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?
          > >[/color][/color]

          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


          Comment

          • tom_usenet

            #6
            Re: partial specialization - why is this NOT ambiguous?

            On Sun, 24 Aug 2003 00:35:21 GMT, "sks_cpp" <sksjava@hotmai l.com>
            wrote:
            [color=blue]
            >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<in t, bool> a;
            >
            >Shouldn't that be ambiguous?[/color]

            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

            Comment

            Working...