Do all members of partial specializations need to be redefined?

M

Martin

I have a couple of partial specializations of a templated class, but
my code results in "undefined reference" errors by the linker.

Can't a partial specialization reuse member functions of the generic
template definition? If not, is there any way to avoid the great
amount of copy-paste code involved in copying the generic member
definitions into the partial specializations?

template<int D, class F>
struct L
{
void f();
void g();
};

template<int D, class F>
void L<D,F>::f()
{
// common code
}

template<class F>
struct L<3,F>
{
void f();
void g();
};

template<class F>
void L<3,F>::g()
{
// partial specialization
}

int main( )
{
L<3,int> l;
l.f();
return 0;
}
 
B

Bo Persson

Martin wrote:
:: I have a couple of partial specializations of a templated class,
:: but my code results in "undefined reference" errors by the linker.
::
:: Can't a partial specialization reuse member functions of the
:: generic template definition? If not, is there any way to avoid the
:: great amount of copy-paste code involved in copying the generic
:: member definitions into the partial specializations?
::

A specialization is totally separate from the non-specialized version.

If you want to share common code, put that in a base class that both
can inherit from.


Bo Persson
 

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
473,774
Messages
2,569,596
Members
45,134
Latest member
Lou6777736
Top