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