Can't figure out specialziation syntax for g++.

J

John

Heya -

The following works with MSVC:

template< bool Boolean >
struct Foo
{
template< class T2 >
void bar( T2 arg )
{
std::cout << arg << std::endl;
}

template<>
void bar( bool arg )
{
if ( Boolean )
std::cout << arg << std::endl;
}

};

Of course, g++ complains about "explicit specialization in non-namespace
scope" - this is usually no big deal, just move the specialization
outside of th class, right? For this example, I'm unable to figure out
how to move the specialization outside of the class definition without
generating a raft of errors, with either compiler. Basically I think
this is some type of partial specialization, but the wrench is the
'bool' as the template parameter of the class.

The closest I've been able to come up with is:

template<>
template< bool Boolean >
inline void Foo<bool>::bar( bool arg )
{
}

but it doesn't seem to work.

Any help would be greatly appreciated. :)

- John
 
J

John Carson

John said:
Heya -

The following works with MSVC:

template< bool Boolean >
struct Foo
{
template< class T2 >
void bar( T2 arg )
{
std::cout << arg << std::endl;
}

template<>
void bar( bool arg )
{
if ( Boolean )
std::cout << arg << std::endl;
}

};

Of course, g++ complains about "explicit specialization in
non-namespace scope" - this is usually no big deal, just move the
specialization outside of th class, right? For this example, I'm
unable to figure out how to move the specialization outside of the
class definition without generating a raft of errors, with either
compiler. Basically I think this is some type of partial
specialization, but the wrench is the 'bool' as the template
parameter of the class.
The closest I've been able to come up with is:

template<>
template< bool Boolean >
inline void Foo<bool>::bar( bool arg )
{
}

but it doesn't seem to work.

Any help would be greatly appreciated. :)


What you are trying to do is not allowed by the standard, though VC++ does
allow it for backward compatibility reasons.

According to the standard, you are only allowed to specialise member
templates if the enclosing template class is fully specialised. Something
like this:

template< bool Boolean >
struct Foo
{
template< class T2 >
void bar( T2 arg )
{
std::cout << arg << std::endl;
}
};

template<>
template<>
void Foo<true>::bar<bool>( bool arg )
{
// Foo<true> case
}

template<>
template<>
void Foo<false>::bar<bool>( bool arg )
{
// Foo<false> case
}
 
J

John

John said:
According to the standard, you are only allowed to specialise member
templates if the enclosing template class is fully specialised. Something
like this:

Awsome! I can deal with that.

Thanks!
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top