Template specialization

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

    #1

    Template specialization

    I'm trying to do some template specialization and can't get it to work
    quite right. I have a templated base class:

    template <typename T> class SignalBase {
    ...
    };

    and a derived class:

    template <typename T> class Signal : public SignalBase<T> {
    ...
    };

    Now I want to do some specialization. This kind works:

    template <> class Signal<uint64> : public SignalBase<uint 64> {
    ...
    };

    but when I try to do a sort of templated-specialization thing:

    template <unsigned WIDTH> class Signal<Bits<WID TH> > : public
    SignalBase<Bits <WIDTH> > {
    ...
    };

    and declare a variable with it:

    Signal<Bits<12> > someSignal;

    the compiler "ignores" this specialization in favor of using the
    original derived class. Any suggestions?

    Scott

  • Alf P. Steinbach

    #2
    Re: Template specialization

    * Scott Frazer:[color=blue]
    > I'm trying to do some template specialization and can't get it to work
    > quite right. I have a templated base class:
    >
    > template <typename T> class SignalBase {
    > ...
    > };
    >
    > and a derived class:
    >
    > template <typename T> class Signal : public SignalBase<T> {
    > ...
    > };
    >
    > Now I want to do some specialization. This kind works:
    >
    > template <> class Signal<uint64> : public SignalBase<uint 64> {
    > ...
    > };[/color]

    Of course, uint64 is a type that you have defined somewhere earlier.

    [color=blue]
    > but when I try to do a sort of templated-specialization thing:
    >
    > template <unsigned WIDTH> class Signal<Bits<WID TH> > : public
    > SignalBase<Bits <WIDTH> > {[/color]

    Don't use all UPPERCASE except for macros.

    [color=blue]
    > ...
    > };
    >
    > and declare a variable with it:
    >
    > Signal<Bits<12> > someSignal;[/color]

    Of course, Bits is a template class that you have defined somewhere earlier.

    [color=blue]
    > the compiler "ignores" this specialization in favor of using the
    > original derived class. Any suggestions?[/color]

    Yes, post minimal code that compiles and illustrates the problem.

    For example, the following code, created by copying and pasting the
    snippets shown (including warts and all), and just adding definitions
    for the things you have omitted, compiles and /does not/ illustrate the
    problem:

    template <typename T> class SignalBase {
    };

    template <typename T> class Signal : public SignalBase<T> {
    };

    typedef unsigned __int64 uint64; // Compiler dependent.

    template <> class Signal<uint64> : public SignalBase<uint 64> {
    };

    template< unsigned n > struct Bits {};

    template <unsigned WIDTH> class Signal<Bits<WID TH> > : public
    SignalBase<Bits <WIDTH> > {
    public:
    void foo() {}
    };

    int main()
    {
    Signal<Bits<12> > someSignal;
    someSignal.foo( );
    }

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Victor Bazarov

      #3
      Re: Template specialization

      Scott Frazer wrote:[color=blue]
      > I'm trying to do some template specialization and can't get it to work
      > quite right. I have a templated base class:
      >
      > template <typename T> class SignalBase {
      > ...
      > };
      >
      > and a derived class:
      >
      > template <typename T> class Signal : public SignalBase<T> {
      > ...
      > };
      >
      > Now I want to do some specialization. This kind works:
      >
      > template <> class Signal<uint64> : public SignalBase<uint 64> {
      > ...
      > };
      >
      > but when I try to do a sort of templated-specialization thing:
      >
      > template <unsigned WIDTH> class Signal<Bits<WID TH> > : public
      > SignalBase<Bits <WIDTH> > {
      > ...
      > };
      >
      > and declare a variable with it:
      >
      > Signal<Bits<12> > someSignal;
      >
      > the compiler "ignores" this specialization in favor of using the
      > original derived class. Any suggestions?[/color]

      Try this on your compiler:
      ------------------------------------------------------------
      #include <iostream>

      template<bool tf> struct yes;
      template<> struct yes<false> { enum { answer = 0 }; };
      template<> struct yes<true> { enum { answer = 1 }; };

      template<class T> struct t {
      t(int) { std::cout << "unspecialized\ n"; }
      };

      template<bool b> struct t<yes<b> > {
      t(int) { std::cout << "SPECIALIZE D on " << b << "\n"; }
      };

      int main()
      {
      t<double> td(42);
      t<yes<true> > ty(77);
      t<yes<false> > tn(666);
      }
      ------------------------------------------------------------
      and see what output you get. If you get three different lines, your
      compiler is OK, and you made a mistake in your code, which you failed
      to expose in your post. If it outputs same three lines, change your
      compiler to something that works.

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

      Comment

      • Scott Frazer

        #4
        Re: Template specialization

        For future reference, the problem turned out to be that Bits<> was
        templated using int, not unsigned, so changing to this:

        template <int WIDTH> class Signal<Bits<WID TH> > : public
        SignalBase<Bits <WIDTH> > {
        ...

        };

        solved the problem.

        Comment

        Working...