Where should the body of template specialisations be placed - header or source file?


K

Kevin Frey

Hello,

Let's say I have the following:

template< class T > class MyClass
{
void Func( );
};

and I want to declare a specialisation for Func( ) in the presence of
a particular type, eg:

template< > void MyClass< long >::Func( )
{
// body of function here
}

Whereabouts should the above specialisation be placed - in the header,
or in the source file (with a corresponding prototype declared as
follows):

template< > void MyClass< long >::Func( );

Thanks

Kevin.
 
Ad

Advertisements

A

Artie Gold

Kevin said:
Hello,

Let's say I have the following:

template< class T > class MyClass
{
void Func( );
};

and I want to declare a specialisation for Func( ) in the presence of
a particular type, eg:

template< > void MyClass< long >::Func( )
{
// body of function here
}

Whereabouts should the above specialisation be placed - in the header,
or in the source file (with a corresponding prototype declared as
follows):

template< > void MyClass< long >::Func( );
Unless your compiler supports the `export' keyword (which is most
likely not the case, very few do), you'll have to put it in your header.

HTH,
--ag
 
T

tom_usenet

Hello,

Let's say I have the following:

template< class T > class MyClass
{
void Func( );
};

and I want to declare a specialisation for Func( ) in the presence of
a particular type, eg:

template< > void MyClass< long >::Func( )
{
// body of function here
}

Whereabouts should the above specialisation be placed - in the header,
or in the source file (with a corresponding prototype declared as
follows):

template< > void MyClass< long >::Func( );

If you don't want it to be inline, you have to put the definition in
the source file (but make sure the declaration is in the header to
avoid linker errors/undefined behaviour). If you want it to be inline,
use the inline keyword with the definition and put it in the header.

Basically, function specializations (and explicit instantiations) must
obey the one definition rule, just like non-templates.

Tom
 
Ad

Advertisements

M

Michael Spencer

Hello,

Let's say I have the following:

template< class T > class MyClass
{
void Func( );
};

and I want to declare a specialisation for Func( ) in the presence of
a particular type, eg:

template< > void MyClass< long >::Func( )
{
// body of function here
}

Whereabouts should the above specialisation be placed - in the header,
or in the source file (with a corresponding prototype declared as
follows):

template< > void MyClass< long >::Func( );

Your explicit specialization should be defined in your source file,
and declared in your header file. Or, if you're lazy, you can just
write in one file

template< class T > class MyClass
{
void Func( );
};

template< > void MyClass< long >::Func( )
{
// body of function here
}

and let a program generate your header and source files. See
http://www.lazycplusplus.net. Try it out at
http://www.lazycplusplus.net/cgi-bin/lzzcgi.

Mike
 

Top