473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Namespace specialization

Hi,

I am probably asking for something which was discussed several times,
but all my researches were unsucessfull.

If you look at this code (simplified part of my project):

---------------
template< int dimension > struct Test
{
template< class A > struct assignement
{
template< int I > struct recurse
{
enum { Counter = I + 1 };
static inline void assign( void* v, const A& a )
{
recurse<Counter>::assign( v, a );
}
};

template<> struct recurse<dimension>
{
enum { Counter = dimension + 1 };
static inline void assign( void*, const A& )
{
}
};

static inline void assign( void* v, const A& a )
{
recurse<0>::assign( v, a );
}
};
};

---------

This code compiles fine with vs2005, but not with g++, which gives me
errors on my specialization template<> struct recurse<dimension>:

error: invalid explicit specialization before '>' token
error: explicit specialization in non-namespace scope `struct
Test<dimension>::assignement<A>'
error: enclosing class templates are not explicitly specialized
error: template parameters not used in partial specialization:
error: `A'

Even though i thought this was completely correct, i tried to put it
outside the class definition, but couldnt get it working.

So i am wondering if what i am doing is actually proper c++, or just
something vs allows? And if you would have a work around..

Thanks!

Feb 21 '06 #1
7 5469
> I am probably asking for something which was discussed several times,
but all my researches were unsucessfull.

If you look at this code (simplified part of my project):

---------------
template< int dimension > struct Test
{
template< class A > struct assignement
{
template< int I > struct recurse
{
enum { Counter = I + 1 };
static inline void assign( void* v, const A& a )
{
recurse<Counter>::assign( v, a );
}
};

template<> struct recurse<dimension>
{
enum { Counter = dimension + 1 };
static inline void assign( void*, const A& )
{
}
};

static inline void assign( void* v, const A& a )
{
recurse<0>::assign( v, a );
}
};
};

---------

This code compiles fine with vs2005, but not with g++, which gives me
errors on my specialization template<> struct recurse<dimension>:

error: invalid explicit specialization before '>' token
error: explicit specialization in non-namespace scope `struct
Test<dimension>::assignement<A>'
error: enclosing class templates are not explicitly specialized
error: template parameters not used in partial specialization:
error: `A'

Even though i thought this was completely correct, i tried to put it
outside the class definition, but couldnt get it working.

So i am wondering if what i am doing is actually proper c++, or just
something vs allows? And if you would have a work around..


MSVC is wrong in this case and g++ correct. Explicit template specialization
is forbidden for nested classes.

As partial template specialization is not forbidden, you get around quite
comfortable. I did not test it, if it is not working, I have to dig in my
code:

template< int dimension > struct Test
{
template< class A > struct assignement
{
template< int I, typename T = void > struct recurse
{
enum { Counter = I + 1 };
static inline void assign( void* v, const A& a )
{
recurse<Counter>::assign( v, a );
}
};

template< typename T > struct recurse<dimension, T>
{
enum { Counter = dimension + 1 };
static inline void assign( void*, const A& )
{
}
};

static inline void assign( void* v, const A& a )
{
recurse<0>::assign( v, a );
}
};
};
Feb 21 '06 #2
Thanks Patrick,
I didn't know explicit template specialization is forbidden for nested
classes. Your change works indeed fine, except when i put this part
back into the main code, it looks infinitely (my terminator is not
used).

/kUfa

Feb 21 '06 #3
looks=loops, sorry for the typo

Feb 21 '06 #4
> Thanks Patrick,
I didn't know explicit template specialization is forbidden for nested
classes. Your change works indeed fine, except when i put this part
back into the main code, it looks infinitely (my terminator is not
used).


Hello Kufa,

I can confirm the infinitive loop for VS2003 and gcc. It looks like a bug to
me.

If you can iterate backwards, you can partial specialize to 0, what works
fine.

Patrick
Feb 21 '06 #5
Yes indeed! Thanks alot Patrick!

Feb 21 '06 #6

"Patrick Kowalzick" <pa***************@mapandguide.de> skrev i
meddelandet news:ne********************@proxy.mapandguide.de.. .

This code compiles fine with vs2005, but not with g++, which gives
me
errors on my specialization template<> struct recurse<dimension>:


MSVC is wrong in this case and g++ correct. Explicit template
specialization is forbidden for nested classes.


It's an MS extension, from way back. If you add the Disable Language
Extensions option to the compile, the code will be rejected.
Bo Persson
Feb 22 '06 #7
Actually i could not find this in the msdn:
http://msdn2.microsoft.com/en-us/library/34h23df8.aspx

If i disable the language extensions, it still compiles..

/kUfa

Feb 22 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
5
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
7
by: Kai-Uwe Bux | last post by:
Hi folks, I observed something that puzzles me. When I do namespace xxx { using std::swap; } it appears that xxx::swap and std::swap are not strictly equivalent. In particular, I think...
8
by: Ferdi Smit | last post by:
I've never understood the rationale of allowing partial, but not explicit specialization for classes at non-namespace scope. Ie.: struct A { template <typename T1, typename T2> struct B {}; ...
2
by: Marcin Kalicinski | last post by:
Is it well formed C++ to define free swap function for my custom container in std namespace? cheers, Marcin
11
by: Noah Roberts | last post by:
template < typename T > std::istream & operator >(std::istream & in, std::pair<T,T& p) { in >p.first >p.second; return in; } .... std::istream_iterator< std::pair<size_type, size_type
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
1
by: Knut Stolze | last post by:
Hi, Let's assume I have a namespace that contains a class definition. Now I would like to add a template specialization for std::numeric_limits for the new class. I want to have this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.