Template specialization

S

Scott Frazer

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<uint64> {
...
};

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

template <unsigned WIDTH> class Signal<Bits<WIDTH> > : 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
 
A

Alf P. Steinbach

* Scott Frazer:
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<uint64> {
...
};

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

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

template <unsigned WIDTH> class Signal<Bits<WIDTH> > : public
SignalBase<Bits<WIDTH> > {

Don't use all UPPERCASE except for macros.

...
};

and declare a variable with it:

Signal<Bits<12> > someSignal;

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

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

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<uint64> {
};

template< unsigned n > struct Bits {};

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

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

Victor Bazarov

Scott said:
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<uint64> {
...
};

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

template <unsigned WIDTH> class Signal<Bits<WIDTH> > : 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?

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 << "SPECIALIZED 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
 
S

Scott Frazer

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<WIDTH> > : public
SignalBase<Bits<WIDTH> > {
...

};

solved the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top