Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can't figure out specialziation syntax for g++.

Reply
Thread Tools

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

 
 
John
Guest
Posts: n/a
 
      11-30-2006
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

 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      11-30-2006
"John" <(E-Mail Removed)> wrote in message
newsyBbh.10346$(E-Mail Removed)
> 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
}


--
John Carson



 
Reply With Quote
 
 
 
 
John
Guest
Posts: n/a
 
      11-30-2006
John Carson wrote:
> "John" <(E-Mail Removed)> wrote in message
> newsyBbh.10346$(E-Mail Removed)
>> Heya -

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I can not figure this vhdl logic out, help. logia VHDL 6 01-16-2006 03:03 PM
Can't figure out syntax error with templates/member function pointers Aaron Walker C++ 4 10-01-2005 04:19 PM
Can't figure out how to put shortcut on desktop in FireFox JohnF Firefox 3 09-15-2004 01:22 PM
can't figure out syntax prob python newbie Python 2 11-22-2003 03:28 AM
Help!! I can't figure out the correct syntax Tim ASP General 2 08-01-2003 06:34 PM



Advertisments