Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Partial Specialization workaround

Reply
Thread Tools

Partial Specialization workaround

 
 
Philip Lawatsch
Guest
Posts: n/a
 
      07-16-2003
Hi

I'd like to implement some kind if type traits myself, but I have to
support broken compilers (like visual studio) that do not support
Partial Specialization.

My first shot was something like this:

----8<---
typedef char IsPODForListArrayTrue;
typedef struct {char bla[2];} IsPODForListArrayFalse;

IsPODForListArrayTrue IsPODForListArrayDummy(int);
IsPODForListArrayTrue IsPODForListArrayDummy(long);

IsPODForListArrayFalse IsPODForListArrayDummy(...);

#define IsPODForListArray(x) (sizeof(IsPODForListArrayDummy(x)) ==
sizeof(IsPODForListArrayTrue))

-----8<----

This seems to work, BUT, the problem here is that I cant just pass a
typename to IsPodForListArray but rather have to pass something real.

eg. IsPodForListArray (int) <-- wont work

int foo; IsPodForListArray(foo) <-- works

This is not acceptable, since I do want to use this in places where i
can only use types (eg template parameters).

I do want to implement it myself, and not add any non std. libs.


Any ideas of what I could do ?
I somehow have to work around the need for partial specialization ..


with kind regards Philip

 
Reply With Quote
 
 
 
 
Philip Lawatsch
Guest
Posts: n/a
 
      07-16-2003

> Like this?
>


<snip code>


Yea, like this, only that this is partial specialization ....


with kind regards Philip

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-16-2003
On Wed, 16 Jul 2003 17:46:55 +0200, Philip Lawatsch <(E-Mail Removed)> wrote:

>Hi
>
>I'd like to implement some kind if type traits myself, but I have to
>support broken compilers (like visual studio) that do not support
>Partial Specialization.
>
>My first shot was something like this:
>
>----8<---
>typedef char IsPODForListArrayTrue;
>typedef struct {char bla[2];} IsPODForListArrayFalse;
>
>IsPODForListArrayTrue IsPODForListArrayDummy(int);
>IsPODForListArrayTrue IsPODForListArrayDummy(long);
>
>IsPODForListArrayFalse IsPODForListArrayDummy(...);
>
>#define IsPODForListArray(x) (sizeof(IsPODForListArrayDummy(x)) ==
>sizeof(IsPODForListArrayTrue))
>
>-----8<----
>
>This seems to work, BUT, the problem here is that I cant just pass a
>typename to IsPodForListArray but rather have to pass something real.
>
>eg. IsPodForListArray (int) <-- wont work
>
>int foo; IsPodForListArray(foo) <-- works
>
>This is not acceptable, since I do want to use this in places where i
>can only use types (eg template parameters).
>
>I do want to implement it myself, and not add any non std. libs.
>
>
>Any ideas of what I could do ?
>I somehow have to work around the need for partial specialization ..


I fail to see where partial specialization enters the picture.

Off the cuff:


template< typename T >
struct IsBasicType{ enum{ value = 0 }; };

template<> struct IsBasicType<int> { enum{ value = 1 }; };
template<> struct IsBasicType<long> { enum{ value = 1 }; };


Checking for POD'ness is much more involved, but still I don't
see the partial specialization (which is a specialization where
just a subset of the template arguments are bound).

 
Reply With Quote
 
Philip Lawatsch
Guest
Posts: n/a
 
      07-16-2003


John Harrison wrote:


>
> I don't understand. I understand what partial specialization, but my code
> didn't use partial specialization so I don't see what the problem with it
> is.


Yea, just realized this !
Sorry, i just saw "template" and didnt read much further, this was
really my fault!

Your code works fine for what i need, so thanks a lot !

with kind regards philip

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-16-2003

"Philip Lawatsch" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> > Like this?
> >

>
> <snip code>
>
>
> Yea, like this, only that this is partial specialization ....
>
>
> with kind regards Philip
>


I don't understand. I understand what partial specialization, but my code
didn't use partial specialization so I don't see what the problem with it
is.

Perhaps if you posted the code that needs to distinguish between POD and
non-POD. Nested template types are often a solution to the lack of partial
specialization.

john


 
Reply With Quote
 
Philip Lawatsch
Guest
Posts: n/a
 
      07-16-2003


John Harrison wrote:

> I don't understand. I understand what partial specialization, but my code
> didn't use partial specialization so I don't see what the problem with it
> is.
>
> Perhaps if you posted the code that needs to distinguish between POD and
> non-POD. Nested template types are often a solution to the lack of partial
> specialization.


One little confusion on my side though
What i have now is:

#define IsPODForListArrayDefine(x) template <>\
class IsPODForListArrayDummy<x>\
{\
public:\
enum {Result = FC_True};\
};

template <class T>
class IsPODForListArrayDummy
{
public:
enum { Result = false };
};

IsPODForListArrayDefine(FC_Int)
IsPODForListArrayDefine(FC_Uint)


#undef IsPODForListArrayDefine

#define IsPODForListArray(x) (IsPODForListArrayDummy<x>::Result)


and this works fine.

But now it will get tricky:

template <class T> class foo;

template <class T>
class IsPODForListArrayDummy < foo <T> >
{
public:
enum {result = FC_True};
};

This wont work, so what would I want to do in this case ?

with kind regards philip

 
Reply With Quote
 
Philip Lawatsch
Guest
Posts: n/a
 
      07-16-2003


Andrey Tarasevich wrote:

> Philip Lawatsch wrote:
>
>>...
>>Yea, like this, only that this is partial specialization ....
>>...

>
>
> Where??? There's no partial specialization in this code. This is
> _explicit_ specialization, which is supported by MSVC++ 6.0 compiler (if
> that's the one you are talking about).


As I said 5 minutes later, i apologize for not thinking while reading
his code. My fault


with kind regards philip

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      07-16-2003
Philip Lawatsch wrote:
> ...
> Yea, like this, only that this is partial specialization ....
> ...


Where??? There's no partial specialization in this code. This is
_explicit_ specialization, which is supported by MSVC++ 6.0 compiler (if
that's the one you are talking about).

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-16-2003

"Philip Lawatsch" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>
> John Harrison wrote:
>
> > I don't understand. I understand what partial specialization, but my

code
> > didn't use partial specialization so I don't see what the problem with

it
> > is.
> >
> > Perhaps if you posted the code that needs to distinguish between POD and
> > non-POD. Nested template types are often a solution to the lack of

partial
> > specialization.

>
> One little confusion on my side though
> What i have now is:
>
> #define IsPODForListArrayDefine(x) template <>\
> class IsPODForListArrayDummy<x>\
> {\
> public:\
> enum {Result = FC_True};\
> };
>
> template <class T>
> class IsPODForListArrayDummy
> {
> public:
> enum { Result = false };
> };
>
> IsPODForListArrayDefine(FC_Int)
> IsPODForListArrayDefine(FC_Uint)
>
>
> #undef IsPODForListArrayDefine
>
> #define IsPODForListArray(x) (IsPODForListArrayDummy<x>::Result)
>
>
> and this works fine.
>
> But now it will get tricky:
>
> template <class T> class foo;
>
> template <class T>
> class IsPODForListArrayDummy < foo <T> >
> {
> public:
> enum {result = FC_True};
> };
>
> This wont work, so what would I want to do in this case ?
>
> with kind regards philip
>


You want to say that any foo<T> is POD? That seems strange because if T is
not POD how can foo<T> be? Indeed can any template be POD? Not sure of the
answer to that one.

In any case looks like you need partial template specialisation again.

john


 
Reply With Quote
 
Philip Lawatsch
Guest
Posts: n/a
 
      07-16-2003


John Harrison wrote:

>
> You want to say that any foo<T> is POD? That seems strange because if T is
> not POD how can foo<T> be? Indeed can any template be POD? Not sure of the
> answer to that one.


Well, foo is actually a coordinate class, which only has 3 T inside.
Anything besides PODs would not make sense in this class (and only these
are used)

I know that this is not nice, but i'm currently speed hacking our array
class and got huge performance increase (>300%) when using malloc et al
for these coordinates in comparison to c++ new [newsize]and then copy.


> In any case looks like you need partial template specialisation again.

Yea, but I found a workaround.
I typedef'd some of the most likely cases (like double, float and int)
and used these.

Not nice but anyway.

Btw, i've added stuff like

if (IsPOD(T))
{
// speed hack code here
}
else
{
// old code here
}

into the array.

Now the array seems to be slower than befor for the old data types, any
ideas why ?

I thought the compiler would emit the same code as befor for the non pod
types since IsPOD is a compile time decision and every normal
optimizer should get rid of that .. ?


with kind regarsd philip

 
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
template specialization overriding non-specialization? Joseph Turian C++ 2 04-16-2006 02:46 PM
partial specialization - why is this NOT ambiguous? sks_cpp C++ 8 08-26-2003 09:28 AM
Partial member of class template specialization Petre Iantu C++ 1 08-17-2003 06:10 PM
partial specialization of function template Alexander Stippler C++ 1 08-15-2003 11:25 AM
template partial specialization Chandra Shekhar Kumar C++ 17 06-25-2003 03:35 PM



Advertisments