By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

separating delcaration and definition of full template specialisation - how?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.