specialization of member functions of class templates

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

    specialization of member functions of class templates

    Hi,

    From what I've read in several places, it seems that explicit specialization
    of member functions of class templates is allowed, but partial
    specialization isn't:

    template<class T, class R> class A {
    void foo();
    }

    template <>
    void A<int,double>:: foo() {} // allowed

    template<class T>
    void A<T,double>::fo o() {} // forbidden



    However, when looking for justification in the ISO standard (final draft,
    1996), it seems the first isn't explicitly allowed in there, and neither is
    the second explicitly forbidden, even though the first seems to be used in
    an example in 14.5.4.3

    So does anyone know what the story is then? Does the standard really
    allow/forbid this or not?

    Thanks,

    Hans
  • Victor Bazarov

    #2
    Re: specialization of member functions of class templates

    "SainTiss" <stiss@gmx.ne t> wrote...[color=blue]
    > From what I've read in several places, it seems that explicit[/color]
    specialization[color=blue]
    > of member functions of class templates is allowed, but partial
    > specialization isn't:
    >
    > template<class T, class R> class A {
    > void foo();
    > }
    >
    > template <>
    > void A<int,double>:: foo() {} // allowed
    >
    > template<class T>
    > void A<T,double>::fo o() {} // forbidden[/color]

    No, not forbidden. You just have to partially specialise the class
    template as well, before you attempt to define the member. 14.5.4.3/1.
    [color=blue]
    > However, when looking for justification in the ISO standard (final draft,
    > 1996), it seems the first isn't explicitly allowed in there, and neither[/color]
    is[color=blue]
    > the second explicitly forbidden, even though the first seems to be used in
    > an example in 14.5.4.3
    >
    > So does anyone know what the story is then? Does the standard really
    > allow/forbid this or not?[/color]

    Both are allowed. A partial specialisation of a member requires
    the same partial specialisation of the enclosing template to exist.
    At least that's how I read the Standard.

    Victor


    Comment

    • SainTiss

      #3
      Re: specialization of member functions of class templates

      Victor Bazarov wrote:
      [color=blue]
      >
      > No, not forbidden. You just have to partially specialise the class
      > template as well, before you attempt to define the member. 14.5.4.3/1.[/color]

      "The template argument list of a member of a class template partial
      specialization shall match the template argument list of the class template
      partial specialization. A class template specialization is a distinct
      template."

      Is this why you conclude that the partially specialised class needs to be
      defined before its member is defined? That's not really clear, is it?

      Additionally, that doesn't explain at all why it *is* allowed for an
      explicitly specialised class...

      Is the standard really this unclear about this?

      Thanks,

      Hans

      Comment

      • Victor Bazarov

        #4
        Re: specialization of member functions of class templates

        "SainTiss" <stiss@gmx.ne t> wrote...[color=blue]
        > Victor Bazarov wrote:
        >[color=green]
        > >
        > > No, not forbidden. You just have to partially specialise the class
        > > template as well, before you attempt to define the member. 14.5.4.3/1.[/color]
        >
        > "The template argument list of a member of a class template partial
        > specialization shall match the template argument list of the class[/color]
        template[color=blue]
        > partial specialization. A class template specialization is a distinct
        > template."
        >
        > Is this why you conclude that the partially specialised class needs to be
        > defined before its member is defined? That's not really clear, is it?[/color]

        I am not sure about clarity (or lack thereof). A partial specialisation
        is a whole different template, and its members know nothing about the
        members of the original template, whereas a full specialisation of
        a member _relies_ on the implicit instantiation of the original template.
        That would be the main difference, I think.
        [color=blue]
        > Additionally, that doesn't explain at all why it *is* allowed for an
        > explicitly specialised class...[/color]

        Because a full specialisation causes an implicit instantiation of the
        original template (I am still looking for the standard phrasing on that).
        [color=blue]
        >
        > Is the standard really this unclear about this?[/color]

        For more clarification of the Standard and why it's so unclear I strongly
        recommend asking in comp.std.c++.

        Victor


        Comment

        • SainTiss

          #5
          Re: specialization of member functions of class templates

          Victor Bazarov wrote:
          [color=blue]
          >
          > I am not sure about clarity (or lack thereof). A partial specialisation
          > is a whole different template, and its members know nothing about the
          > members of the original template, whereas a full specialisation of
          > a member _relies_ on the implicit instantiation of the original template.
          > That would be the main difference, I think.[/color]
          [color=blue]
          >[color=green]
          >> Additionally, that doesn't explain at all why it *is* allowed for an
          >> explicitly specialised class...[/color]
          >
          > Because a full specialisation causes an implicit instantiation of the
          > original template (I am still looking for the standard phrasing on that).[/color]


          That would indeed be a satisfactory explanation, if it is backed by the
          standard. So I'm looking into that as well, but although it is possible to
          interpret certain parts of it in that way, so far I too failed to find a
          part which clearly justifies this.

          Thanks,

          Hans

          [color=blue]
          >
          > Victor[/color]

          Comment

          Working...