Partial Specialization workaround

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philip Lawatsch

    Partial Specialization workaround

    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 IsPODForListArr ayTrue;
    typedef struct {char bla[2];} IsPODForListArr ayFalse;

    IsPODForListArr ayTrue IsPODForListArr ayDummy(int);
    IsPODForListArr ayTrue IsPODForListArr ayDummy(long);

    IsPODForListArr ayFalse IsPODForListArr ayDummy(...);

    #define IsPODForListArr ay(x) (sizeof(IsPODFo rListArrayDummy (x)) ==
    sizeof(IsPODFor ListArrayTrue))

    -----8<----

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

    eg. IsPodForListArr ay (int) <-- wont work

    int foo; IsPodForListArr ay(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

    #2
    Re: Partial Specialization workaround

    [color=blue]
    > Like this?
    >[/color]

    <snip code>


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


    with kind regards Philip

    Comment

    • Alf P. Steinbach

      #3
      Re: Partial Specialization workaround

      On Wed, 16 Jul 2003 17:46:55 +0200, Philip Lawatsch <philip@waug.at > wrote:
      [color=blue]
      >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 IsPODForListArr ayTrue;
      >typedef struct {char bla[2];} IsPODForListArr ayFalse;
      >
      >IsPODForListAr rayTrue IsPODForListArr ayDummy(int);
      >IsPODForListAr rayTrue IsPODForListArr ayDummy(long);
      >
      >IsPODForListAr rayFalse IsPODForListArr ayDummy(...);
      >
      >#define IsPODForListArr ay(x) (sizeof(IsPODFo rListArrayDummy (x)) ==
      >sizeof(IsPODFo rListArrayTrue) )
      >
      >-----8<----
      >
      >This seems to work, BUT, the problem here is that I cant just pass a
      >typename to IsPodForListArr ay but rather have to pass something real.
      >
      >eg. IsPodForListArr ay (int) <-- wont work
      >
      >int foo; IsPodForListArr ay(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 ..[/color]

      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<lon g> { 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).

      Comment

      • John Harrison

        #4
        Re: Partial Specialization workaround


        "Philip Lawatsch" <philip@waug.at > wrote in message
        news:3f15780a@e-post.inode.at.. .[color=blue]
        >[color=green]
        > > Like this?
        > >[/color]
        >
        > <snip code>
        >
        >
        > Yea, like this, only that this is partial specialization ....
        >
        >
        > with kind regards Philip
        >[/color]

        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


        Comment

        • Philip Lawatsch

          #5
          Re: Partial Specialization workaround



          John Harrison wrote:

          [color=blue]
          >
          > 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.[/color]

          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

          Comment

          • Philip Lawatsch

            #6
            Re: Partial Specialization workaround



            John Harrison wrote:
            [color=blue]
            > 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.[/color]

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

            #define IsPODForListArr ayDefine(x) template <>\
            class IsPODForListArr ayDummy<x>\
            {\
            public:\
            enum {Result = FC_True};\
            };

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

            IsPODForListArr ayDefine(FC_Int )
            IsPODForListArr ayDefine(FC_Uin t)


            #undef IsPODForListArr ayDefine

            #define IsPODForListArr ay(x) (IsPODForListAr rayDummy<x>::Re sult)


            and this works fine.

            But now it will get tricky:

            template <class T> class foo;

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

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

            with kind regards philip

            Comment

            • Andrey Tarasevich

              #7
              Re: Partial Specialization workaround

              Philip Lawatsch wrote:[color=blue]
              > ...
              > Yea, like this, only that this is partial specialization ....
              > ...[/color]

              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

              Comment

              • Philip Lawatsch

                #8
                Re: Partial Specialization workaround



                Andrey Tarasevich wrote:
                [color=blue]
                > Philip Lawatsch wrote:
                >[color=green]
                >>...
                >>Yea, like this, only that this is partial specialization ....
                >>...[/color]
                >
                >
                > 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).[/color]

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


                with kind regards philip

                Comment

                • John Harrison

                  #9
                  Re: Partial Specialization workaround


                  "Philip Lawatsch" <philip@waug.at > wrote in message
                  news:3f157d49@e-post.inode.at.. .[color=blue]
                  >
                  >
                  > John Harrison wrote:
                  >[color=green]
                  > > I don't understand. I understand what partial specialization, but my[/color][/color]
                  code[color=blue][color=green]
                  > > didn't use partial specialization so I don't see what the problem with[/color][/color]
                  it[color=blue][color=green]
                  > > 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[/color][/color]
                  partial[color=blue][color=green]
                  > > specialization.[/color]
                  >
                  > One little confusion on my side though
                  > What i have now is:
                  >
                  > #define IsPODForListArr ayDefine(x) template <>\
                  > class IsPODForListArr ayDummy<x>\
                  > {\
                  > public:\
                  > enum {Result = FC_True};\
                  > };
                  >
                  > template <class T>
                  > class IsPODForListArr ayDummy
                  > {
                  > public:
                  > enum { Result = false };
                  > };
                  >
                  > IsPODForListArr ayDefine(FC_Int )
                  > IsPODForListArr ayDefine(FC_Uin t)
                  >
                  >
                  > #undef IsPODForListArr ayDefine
                  >
                  > #define IsPODForListArr ay(x) (IsPODForListAr rayDummy<x>::Re sult)
                  >
                  >
                  > and this works fine.
                  >
                  > But now it will get tricky:
                  >
                  > template <class T> class foo;
                  >
                  > template <class T>
                  > class IsPODForListArr ayDummy < foo <T> >
                  > {
                  > public:
                  > enum {result = FC_True};
                  > };
                  >
                  > This wont work, so what would I want to do in this case ?
                  >
                  > with kind regards philip
                  >[/color]

                  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


                  Comment

                  • Philip Lawatsch

                    #10
                    Re: Partial Specialization workaround



                    John Harrison wrote:
                    [color=blue]
                    >
                    > 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.[/color]

                    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.

                    [color=blue]
                    > In any case looks like you need partial template specialisation again.[/color]
                    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

                    Comment

                    Working...