Partial Template Specialization

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

    Partial Template Specialization

    Hi group,

    I have a problem with partial template specialization. In the code
    below I have a template struct Music with one method, play(),
    and three kinds of music, Jazz, Funk and Bach. When I specialize
    Music<Bach>, I expect that the original play() method is available
    in the specialization, but it is not. How can I fix this?

    -X

    struct Jazz{};
    struct Funk{};
    struct Bach{};
    template<struct A>struct Music{void play(){}};
    struct Music<Bach>{}; //partial template specialization
    int main()
    {
    Music<Jazz>().p lay(); //OK
    Music<Funk>().p lay(); //OK
    Music<Bach>().p lay(); //error, Music<Bach>().p lay not declared
    return 0;
    }



  • Dave

    #2
    Re: Partial Template Specialization


    "Agent Mulder" <mbmulder_remov e_this_@home.nl > wrote in message
    news:bolg5e$tou $1@news4.tilbu1 .nb.home.nl...[color=blue]
    > Hi group,
    >
    > I have a problem with partial template specialization. In the code
    > below I have a template struct Music with one method, play(),
    > and three kinds of music, Jazz, Funk and Bach. When I specialize
    > Music<Bach>, I expect that the original play() method is available
    > in the specialization, but it is not. How can I fix this?
    >
    > -X
    >
    > struct Jazz{};
    > struct Funk{};
    > struct Bach{};
    > template<struct A>struct Music{void play(){}};
    > struct Music<Bach>{}; //partial template specialization
    > int main()
    > {
    > Music<Jazz>().p lay(); //OK
    > Music<Funk>().p lay(); //OK
    > Music<Bach>().p lay(); //error, Music<Bach>().p lay not declared
    > return 0;
    > }[/color]

    I'm sure others will be able to give a more complete answer than I, but in
    looking at your code, your specialization for Bach does indeed not have a
    play() member function. In fact, it doesn't have any member functions or
    data members - it's an empty class! I believe I'm correct in saying that
    your partial specialization will have only what you explicitly put in it.
    I'll rely on others to correct me if I'm wrong, but I think what I said is
    correct...

    Also, template<struct A> should be either template<typena me A> or
    template<class A> (these two are equivalent).


    Comment

    • Andrey Tarasevich

      #3
      Re: Partial Template Specialization

      Agent Mulder wrote:[color=blue]
      > ...
      > I have a problem with partial template specialization. In the code
      > below I have a template struct Music with one method, play(),
      > and three kinds of music, Jazz, Funk and Bach. When I specialize
      > Music<Bach>, I expect that the original play() method is available
      > in the specialization, but it is not. How can I fix this?
      > ...
      > struct Jazz{};
      > struct Funk{};
      > struct Bach{};
      > template<struct A>struct Music{void play(){}};
      > struct Music<Bach>{}; //partial template specialization[/color]

      This is not partial specialization. This is explicit specialization. And
      the correct syntax is a s follows

      template<> struct Music<Bach> {};
      [color=blue]
      > int main()
      > {
      > Music<Jazz>().p lay(); //OK
      > Music<Funk>().p lay(); //OK
      > Music<Bach>().p lay(); //error, Music<Bach>().p lay not declared
      > return 0;
      > }
      > ...[/color]

      Explicit (or partial) specialization of a template is an independently
      defined template. You defined it as a template class with no explicit
      members. That's what you get when you instantiate the specialized
      version - a class with no explicit members. It doesn't have method
      'play()', hence the error.

      If you want to have an explicit specialization of the entire
      'Music<Bach>' and still have method 'play' in it, you have no other
      choice but to declare and define this method in 'Music<Bach>' explicitly.

      template<> struct Music<Bach> { void play( /* whatever */ ); };

      On the other hand, if you just want to explicitly specialize the method
      'play' for each kind of music, there is no need to explicitly specialize
      the entire template 'Music'

      struct Jazz {};
      struct Funk {};
      struct Bach {};

      template<struct A> struct Music { void play(){} };

      // Explicit specializations for method 'play()'
      template<> void Music<Jazz>::pl ay() { /* whatever 1 */ }
      template<> void Music<Funk>::pl ay() { /* whatever 2 */ }
      template<> void Music<Bach>::pl ay() { /* whatever 3 */ }

      --
      Best regards,
      Andrey Tarasevich

      Comment

      • Andrey Tarasevich

        #4
        Re: Partial Template Specialization

        Agent Mulder wrote:[color=blue]
        > ...
        > template<struct A>struct Music{void play(){}};
        > ...[/color]

        BTW, something I haven't noticed right away: you cannot use keyword
        'struct' to declare template parameters of a template. The only keywords
        that can be used for this purpose are 'typename' and 'class'.

        By using keyword 'struct' you are actually making an attempt to declare
        a non-type parameter of type 'struct A'. This is illegal. Firstly,
        non-type template parameters of class type are not allowed in C++. And
        secondly, you don't even have a struct named 'A' in your program.

        --
        Best regards,
        Andrey Tarasevich

        Comment

        • Cy Edmunds

          #5
          Re: Partial Template Specialization

          "Agent Mulder" <mbmulder_remov e_this_@home.nl > wrote in message
          news:bolg5e$tou $1@news4.tilbu1 .nb.home.nl...[color=blue]
          > Hi group,
          >
          > I have a problem with partial template specialization. In the code
          > below I have a template struct Music with one method, play(),
          > and three kinds of music, Jazz, Funk and Bach. When I specialize
          > Music<Bach>, I expect that the original play() method is available
          > in the specialization, but it is not. How can I fix this?
          >
          > -X
          >
          > struct Jazz{};
          > struct Funk{};
          > struct Bach{};
          > template<struct A>struct Music{void play(){}};
          > struct Music<Bach>{}; //partial template specialization
          > int main()
          > {
          > Music<Jazz>().p lay(); //OK
          > Music<Funk>().p lay(); //OK
          > Music<Bach>().p lay(); //error, Music<Bach>().p lay not declared
          > return 0;
          > }
          >[/color]


          Maybe they use templates this way on the X-Files but... hehe

          I would suggest this design instead:

          class Music
          {
          public:
          virtual void play() = 0;
          virtual ~Music() {}
          };

          class Jazz : public Music
          {
          public:
          virtual void play();
          };

          class Funk: public Music
          {
          public:
          virtual void play();
          };

          etc.

          --
          Cy



          Comment

          • Agent Mulder

            #6
            Re: Partial Template Specialization

            Thank you for your insightful responses. I fixed the design
            by letting the template base and the template specialization
            both inherit from the same class CD. This class CD has a virtual
            play() method that gets called through a reference.

            Still I am not satisfied. First, I need to forward declare struct
            Bach. Second, both templates need to inherit class CD. What's the
            use of template specialization if you lose everything that was
            specified in the base template?

            #include<iostre am>
            struct Bach; //forward declaration
            struct CD{virtual void play(){cout<<"\ nFeep-Honk-Feep-Honk";}};
            template<class A>struct Music:CD{};
            struct Music<Bach>:CD{ void play(){cout<<"\ nHonk-Honk-Feep-Feep";}};
            struct Jazz{};
            struct Funk{};
            struct Bach{};
            Music<Jazz>jazz ;
            Music<Funk>funk ;
            Music<Bach>bach ;
            int main()
            {
            CD&jazz=::jazz ;
            CD&funk=::funk ;
            CD&bach=::bach ;
            jazz.play();
            funk.play();
            bach.play();
            return 0;
            }

            -------------------
            output:
            Feep-Honk-Feep-Honk
            Feep-Honk-Feep-Honk
            Honk-Honk-Feep-Feep

            -X


            Comment

            • Cy Edmunds

              #7
              Re: Partial Template Specialization

              "Agent Mulder" <mbmulder_remov e_this_@home.nl > wrote in message
              news:bonvtk$grt $1@news1.tilbu1 .nb.home.nl...[color=blue]
              > Thank you for your insightful responses. I fixed the design
              > by letting the template base and the template specialization
              > both inherit from the same class CD. This class CD has a virtual
              > play() method that gets called through a reference.
              >
              > Still I am not satisfied. First, I need to forward declare struct
              > Bach. Second, both templates need to inherit class CD. What's the
              > use of template specialization if you lose everything that was
              > specified in the base template?[/color]

              What indeed? You don't need any templates to do what you are trying to do.
              They just confuse the issue.

              --
              Cy



              Comment

              • Agent Mulder

                #8
                Re: Partial Template Specialization

                "Cy Edmunds" <cedmunds@spaml ess.rochester.r r.com> wrote in message news:a0Xrb.6462 5$ji3.28841@twi ster.nyroc.rr.c om...[color=blue]
                > "Agent Mulder" <mbmulder_remov e_this_@home.nl > wrote in message
                > news:bonvtk$grt $1@news1.tilbu1 .nb.home.nl...[color=green]
                > > Thank you for your insightful responses. I fixed the design
                > > by letting the template base and the template specialization
                > > both inherit from the same class CD. This class CD has a virtual
                > > play() method that gets called through a reference.
                > >
                > > Still I am not satisfied. First, I need to forward declare struct
                > > Bach. Second, both templates need to inherit class CD. What's the
                > > use of template specialization if you lose everything that was
                > > specified in the base template?[/color]
                >
                > What indeed? You don't need any templates to do what you are trying to do.
                > They just confuse the issue.[/color]

                Templates I try to understand, not the inner working of Music<Bach>::pl ay();
                although you do seem to think so. Refer to Stroustrup 13.6 Derivation and
                Templates for examples of how to organize your code around templates.

                -X

                I try to understand templates I try to understand. Not to implement Music<Jazz>::pl ay(),
                although you do seem to think so.


                Comment

                • Jerry Coffin

                  #9
                  Re: Partial Template Specialization

                  In article <bolg5e$tou$1@n ews4.tilbu1.nb. home.nl>,
                  mbmulder_remove _this_@home.nl says...[color=blue]
                  > Hi group,
                  >
                  > I have a problem with partial template specialization.[/color]

                  First and foremost that what you think is partial specialization is
                  really explicit specialization?

                  Partial specialization looks something like this:

                  template <class A, class B>
                  class X { ... };

                  template <class A>
                  class X<int, A> { ... };

                  The first is unspecialized -- either parameter can potentially be bound
                  to any type. The second is a partial specialization. We're specifying
                  what code will be produced when the first parameter happens to be int,
                  but the second parameter can still vary. The bottom line: to be partial
                  specialization, you have to have at least one each of specified
                  parameters and unspecified parameters. If you're specifying the types
                  of all parameters, you have an explicit specialization instead.
                  [color=blue]
                  > In the code
                  > below I have a template struct Music with one method, play(),
                  > and three kinds of music, Jazz, Funk and Bach. When I specialize
                  > Music<Bach>, I expect that the original play() method is available
                  > in the specialization, but it is not. How can I fix this?[/color]

                  No -- specialization is not inheritance. Each specialization (partial
                  or explicit) is independent of the primary template, and has to define
                  its own members. The members of a specialization may be completely
                  different from the members of the primary template.

                  --
                  Later,
                  Jerry.

                  The universe is a figment of its own imagination.

                  Comment

                  Working...