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.
 
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
 
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
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top