473,414 Members | 1,636 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,414 software developers and data experts.

separating delcaration and definition of full template specialisation - how?

Hi,

I'm currently wondering if and how you can separate
definition from declaration of a full template
specialisation.

I have a UnitTest class that has an _assertEqual member
template that I want to specialize for types from a
3rd-party library. Simplified, the code looks like this:

--test.h--
namespace UnitTest {
class Test {
// ...
protected:
template <typename T, typename S>
void _assertEqual(
const T & x1, const S & x2 /*...*/ )
{
if ( x1 == x2 ) success(); else fail();
}
// ...
};
}

--3rdparty_unittest_helper.h--
#include <unittest/test.h>
class Foo;
namepace UnitTest {
template <>
Test::_assertEqual<Foo,Foo>( const Foo &, const Foo & );
}

--3rdparty_unittest_helper.cpp--
#include "3rdpart_unittest_helper.h"
#include <3rdparty/foo.h>

namespace {
static bool compare_foos(
const Foo & lhs, const Foo & rhs ) {
// determin ordering
}
}

template <>
UnitTest::Test::_assertEqual<Foo,Foo>(
const Foo & x1, const Foo & x2
) {
if ( compare_foos( x1, x2 ) == 0 ) success();
else fail();
}
GCC 4.0 compiles and runs this as (I) expected. The
question is: if this portable, or a GCC extension/bug? Do
I get myself into trouble when I use a static method in
the implementation like the above (I remember such issues
from Sutter's discussion of extern, but this is
different, isn't it? It's a full specialisation, ie. a
normal function, no two-stage lookup involved here,
right?)

Thanks,
Marc

Aug 30 '05 #1
2 1303
Marc Mutz wrote:
....
--3rdparty_unittest_helper.cpp--
#include "3rdpart_unittest_helper.h"
#include <3rdparty/foo.h>

namespace {
static bool compare_foos(
const Foo & lhs, const Foo & rhs ) {
// determin ordering
}
}

template <>
UnitTest::Test::_assertEqual<Foo,Foo>(
const Foo & x1, const Foo & x2
) {
if ( compare_foos( x1, x2 ) == 0 ) success();
else fail();
}
This should be :

namespace UnitTest {
Test::_assertEqual<Foo,Foo>(
const Foo & x1, const Foo & x2
) {
if ( compare_foos( x1, x2 ) == 0 ) success();
else fail();
}
} // namespace



GCC 4.0 compiles and runs this as (I) expected. The
question is: if this portable, or a GCC extension/bug?
This is portable.

Do I get myself into trouble when I use a static method in
the implementation like the above (I remember such issues
from Sutter's discussion of extern, but this is
different, isn't it?
I don't know what Sutter's assertion is.

It's a full specialisation, ie. a normal function, no two-stage lookup involved here,
right?)


Right. It's a full specialization so there is only one time that
lookups can occur.
Aug 30 '05 #2
Gianni Mariani wrote:
template <>
UnitTest::Test::_assertEqual<Foo,Foo>(
const*Foo*&*x1,*const*Foo*&*x2
) {
if*(*compare_foos(*x1,*x2*)*==*0*)*success();
else*fail();
}
This should be :

namespace UnitTest {


+ template<> ?
Test::_assertEqual<Foo,Foo>(
const*Foo*&*x1,*const*Foo*&*x2
) {
if*(*compare_foos(*x1,*x2*)*==*0*)*success();
else*fail();
}
} // namespace

<snip>

Thanks,
Marc

Aug 30 '05 #3

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

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
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: ...
2
by: Martin MacRobert | last post by:
Hi, I'm trying to make a specialisation of a template function, so that the second parameter accepts scalar types only (int,double,float etc.). How can I do this without writing an explicit...
8
by: Paul Roberts | last post by:
Hi, I'm hoping somebody here can help me with a simple problem of template syntax. Here's an example: template<typename T, int iclass A { static int a;
14
by: aaragon | last post by:
Hi everyone, I've been writing some code and so far I have all the code written in the .h files in order to avoid the linker errors. I'm using templates. I wanted to move the implementations to...
8
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
6
by: johnbrown105 | last post by:
Is it possible to force the compiler to use a generic template rather than a matching specialisation? Consider the following: ////////////////////////////////////////// template.cpp starts...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
5
by: ruebezaehler | last post by:
Hello, I should separate the definition and declaration of template code. This works fine for non-specialized templates. But I do not know how to do this for specialized templates. Example: ...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.