separating delcaration and definition of full template specialisation - how?

M

Marc Mutz

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
 
G

Gianni Mariani

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

Marc Mutz

Gianni said:
This should be :

namespace UnitTest {

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

Thanks,
Marc
 

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,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top