473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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.
Jul 19 '05 #1
3 1826
Kevin Frey wrote:
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

--
Artie Gold -- Austin, Texas

Jul 19 '05 #2
On 8 Sep 2003 17:41:41 -0700, ke**********@hotmail.com (Kevin Frey)
wrote:
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
Jul 19 '05 #3
ke**********@hotmail.com (Kevin Frey) wrote in message news:<e5*************************@posting.google.c om>...
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
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Tim Clacy | last post by:
Your expertise will be appreciated... Here's a general templatised class; all specialisations of this class should have a pointer to a specialisation of the same, templatised type: ...
5
by: nifsmith | last post by:
Hi I am trying to learn about Queues and use templates at the same time. I have written the following code and I am getting a link error, stating "unresolved external symbol, "int__cdecl...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
2
by: Joseph Turian | last post by:
I'm posting this question for a friend who lacks USENET access. He and I were discussing this question and could not figure out the solution. Thank you for your help Joseph ===
3
by: Veeru | last post by:
i have one structure SPageDetails. i have a template class CStack. Now i have a pointer of this stack class in another class "CAnotherClass" as: class CAnotherClass {...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
4
by: mathieu | last post by:
Why is the following code causing duplicate definitions (gcc 4.2) when including swapper.h in multiple files ? Thanks -Mathieu --------swapper.h----------- #ifndef swapper_h #define...
5
by: Gernot Frisch | last post by:
// - in my xy.cpp file -- template <const int Ttex, const int Tcol, const int Tlight, int TzBuffer> struct MyFragmentShader { static const int varying_count = Ttex*2 + Tcol*3 + Tlight; }; ...
5
by: aryan | last post by:
Is it not allowed to have template function definition in a .cc file? Here is the scenario. The tempage function declaration is in a header file and the definition in a .cc file. The function is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.