specialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Amadeus W. M.

    specialization

    I have a class

    template <class Vector_t>
    class A;

    where Vector_t is some sort of vector. I want to provide different
    specializations for real and for complex vectors. I need the following:

    // Real vectors:
    A< valarray<double > >; // templated
    A< MyRealVector >; // non-tempalted

    // Complex vectors:
    A< vector< complex<double> >; // templated
    A< MyComplexVector >; // non-templated.

    Is this possible? How would I do that? Bad design? What's the right way?
    Thanks!



  • Victor Bazarov

    #2
    Re: specialization

    Amadeus W. M. wrote:[color=blue]
    > I have a class
    >
    > template <class Vector_t>
    > class A;
    >
    > where Vector_t is some sort of vector. I want to provide different
    > specializations for real and for complex vectors. I need the following:
    >
    > // Real vectors:
    > A< valarray<double > >; // templated[/color]

    What do you mean by the word 'templated' in the comment?
    [color=blue]
    > A< MyRealVector >; // non-tempalted
    >
    > // Complex vectors:
    > A< vector< complex<double> >; // templated
    > A< MyComplexVector >; // non-templated.
    >
    > Is this possible? How would I do that? Bad design? What's the right way?[/color]

    Have you tried? What's the result? If you haven't, what's stopping you?
    What book are you reading that doesn't tell how to specialise a template?

    template<> class A<MyRealVector > { /* specialisation */ };

    or

    typedef A<MyRealVector > MyRealA;

    They give you different results, of course. The former would require you
    to actually specify the inner workings (which are supposedly different
    from the original template), the latter _uses_ the inner workings of the
    original template.

    V
    --
    Please remove capital As from my address when replying by mail

    Comment

    • Amadeus W. M.

      #3
      Re: specialization

      On Fri, 24 Feb 2006 12:31:02 -0500, Victor Bazarov wrote:
      [color=blue]
      > Amadeus W. M. wrote:[color=green]
      >> I have a class
      >>
      >> template <class Vector_t>
      >> class A;
      >>
      >> where Vector_t is some sort of vector. I want to provide different
      >> specializations for real and for complex vectors. I need the following:
      >>
      >> // Real vectors:
      >> A< valarray<double > >; // templated[/color]
      >
      > What do you mean by the word 'templated' in the comment?
      >[color=green]
      >> A< MyRealVector >; // non-tempalted
      >>
      >> // Complex vectors:
      >> A< vector< complex<double> >; // templated
      >> A< MyComplexVector >; // non-templated.[/color][/color]

      Sorry, I didn't explain very well.
      I mean that the template argument of A is itself a template.

      So I have the most general

      template <class Vector_t>
      class A
      {
      };

      I don't want to specialize it to a particular vector type, such as
      valarray<double > or MyNonTemplatedV ector. The specializations differ in
      the SCALAR type of the vector, but should be irrespective of the actual
      container. So what I'd like would be rather

      template <class Scalar_t, template<class> class Vector_t>
      class A
      {
      // work on Vector_t<Scalar _t>;
      };

      Then this should somehow be specialized (1) to Scalar_t = float/double and
      (2) to Scalar_t = complex<double> , for instance, but still have the
      container Vector_t as a parameter.

      I'm reading Stroustrup's book. Can this be done?

      Even if it CAN be done, with a template Vector_t I won't be able to have a

      A<MyNonTemplate dVector>;


      Maybe I should just do

      template <class Vector_t>
      class RealA
      {

      };

      template <class Vector_t>
      class ComplexA
      {

      };

      Then Vector_t is the most general, and I have two implementations - one
      for real, one for complex. It won't be transparent to the user though.


      Comment

      • Greg

        #4
        Re: specialization


        Amadeus W. M. wrote:[color=blue]
        > On Fri, 24 Feb 2006 12:31:02 -0500, Victor Bazarov wrote:
        >[color=green]
        > > Amadeus W. M. wrote:[color=darkred]
        > >> I have a class
        > >>
        > >> template <class Vector_t>
        > >> class A;
        > >>
        > >> where Vector_t is some sort of vector. I want to provide different
        > >> specializations for real and for complex vectors. I need the following:
        > >>
        > >> // Real vectors:
        > >> A< valarray<double > >; // templated[/color]
        > >
        > > What do you mean by the word 'templated' in the comment?
        > >[color=darkred]
        > >> A< MyRealVector >; // non-tempalted
        > >>
        > >> // Complex vectors:
        > >> A< vector< complex<double> >; // templated
        > >> A< MyComplexVector >; // non-templated.[/color][/color]
        >
        > Sorry, I didn't explain very well.
        > I mean that the template argument of A is itself a template.
        >
        > So I have the most general
        >
        > template <class Vector_t>
        > class A
        > {
        > };
        >
        > I don't want to specialize it to a particular vector type, such as
        > valarray<double > or MyNonTemplatedV ector. The specializations differ in
        > the SCALAR type of the vector, but should be irrespective of the actual
        > container. So what I'd like would be rather
        >
        > template <class Scalar_t, template<class> class Vector_t>
        > class A
        > {
        > // work on Vector_t<Scalar _t>;
        > };
        >
        > Then this should somehow be specialized (1) to Scalar_t = float/double and
        > (2) to Scalar_t = complex<double> , for instance, but still have the
        > container Vector_t as a parameter.
        >
        > I'm reading Stroustrup's book. Can this be done?
        >[/color]

        Yes:

        template <class Scalar_t, template<class> class Vector_t>
        class A
        {
        // work on Vector_t<Scalar _t>;

        };

        template < template<class> class Vector_t>
        class A<double, Vector_t>
        {

        };
        [color=blue]
        > Even if it CAN be done, with a template Vector_t I won't be able to have a
        >
        > A<MyNonTemplate dVector>;
        >[/color]

        You can if you declare another "A" class template, in addition to the
        first.

        Greg

        Comment

        Working...