Partial Specialization workaround

Discussion in 'C++' started by Philip Lawatsch, Jul 16, 2003.

  1. 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
     
    Philip Lawatsch, Jul 16, 2003
    #1
    1. Advertising


  2. > Like this?
    >


    <snip code>


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


    with kind regards Philip
     
    Philip Lawatsch, Jul 16, 2003
    #2
    1. Advertising

  3. On Wed, 16 Jul 2003 17:46:55 +0200, Philip Lawatsch <> 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).
     
    Alf P. Steinbach, Jul 16, 2003
    #3
  4. 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
     
    Philip Lawatsch, Jul 16, 2003
    #4
  5. "Philip Lawatsch" <> wrote in message
    news:...
    >
    > > 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
     
    John Harrison, Jul 16, 2003
    #5
  6. 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
     
    Philip Lawatsch, Jul 16, 2003
    #6
  7. 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
     
    Philip Lawatsch, Jul 16, 2003
    #7
  8. 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
     
    Andrey Tarasevich, Jul 16, 2003
    #8
  9. "Philip Lawatsch" <> wrote in message
    news:...
    >
    >
    > 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
     
    John Harrison, Jul 16, 2003
    #9
  10. 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
     
    Philip Lawatsch, Jul 16, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chandra Shekhar Kumar

    template partial specialization

    Chandra Shekhar Kumar, Jun 24, 2003, in forum: C++
    Replies:
    17
    Views:
    3,055
    tom_usenet
    Jun 25, 2003
  2. Alexander Stippler

    partial specialization of function template

    Alexander Stippler, Aug 15, 2003, in forum: C++
    Replies:
    1
    Views:
    558
    Rob Williscroft
    Aug 15, 2003
  3. Petre Iantu
    Replies:
    1
    Views:
    363
    Petre Iantu
    Aug 17, 2003
  4. sks_cpp
    Replies:
    8
    Views:
    354
    tom_usenet
    Aug 26, 2003
  5. Joseph Turian
    Replies:
    2
    Views:
    485
Loading...

Share This Page