Results 1 to 17 of 17

Thread: FYI template partial specialation emulation in MSVC6 now possible

  1. #1

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Talking FYI template partial specialation emulation in MSVC6 now possible

    In an attempt to invent a compiletime nested type sorter (still working on it) I discovered a wonderful way to emulate partial specialation without even having to change the syntax, by actually nest a class with the unspecified parameters Now this case only shows how you can specialize one parameter, similarily if you need to specialize any parameter you have to nest each and one of them into the other and then create a specialized nested class where the needed parameters are specialized accordingly. This is just the beginnig of an extensive use of compiletime type selection, and as already mentioned, i'm doing a recursive sort algoritm that will resolve the type order at compile time
    PHP Code:
    #include <iostream>
    using namespace std;

    template <int x>
    struct Max_Partial{
        
    template <class A,class B>
        
    struct Max_Partial1{
            
    typedef A Max_Partial2;
        };
    };    
    template <>
    struct Max_Partial<0>{
        
    template <class A,class B>
        
    struct Max_Partial1{
            
    typedef A Max_Partial2;
        };
    };    
    struct Max_Partial<1>{
        
    template <class A,class B>
        
    struct Max_Partial1{
            
    typedef B Max_Partial2;
        };
    };    
    template <class A,class B,int x>
    struct Max{
        
    typedef Max_Partial<x>::Max_Partial1<A,B>::Max_Partial2 TMax;
    };


    struct A{
        static const 
    charme(){return "class A";};
    };
    struct B{
        static const 
    charme(){return "class B";};
    };

    void main(){
        
    cout << Max<A,B,1>::TMax::me();
        
    cout << Max<A,B,0>::TMax::me();

    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  2. #2
    jim mcnamara
    Guest
    Oooo.

    But try:
    PHP Code:
    cout<<Max<(void *)a,(void *)b, (void *)c>::Tmax::me(); 
    I get 'unrecoverable compiler error at line 78'

    It really doesn't seem to like void?..... OR did I type wrong?

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    So how would you write the compile time pow now?
    PHP Code:
    template <int a>
    struct cpow {
      
    template <int b>
      
    struct cpow_inner {
        
    enum val cpow<a>::cpow_inner<b-1>::val a};
      };
    };

    template <int a>
     
    template <>
    struct cpow<a>::cpow_inner<0> {
      
    enum val };
    }; 
    Did I get this right?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I don't know what your compiler says about template<int a> template<> kind of declaration but It certainly looks weird, in msvc with the same base you can nest the template specification classes no matter of scope, so it would look something like this:
    PHP Code:
    template <int a>
    struct cpow {
        
    template <int b>
        
    struct cpow_inner {
            
    enum val cpow<a>::cpow_inner<b-1>::val a};
        };

        
    template <>
        
    struct cpow_inner<0> {
            
    enum val };
        };
    }; 
    and a^0 = 1, not a
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    aahh, I didn't know you can write specialisations inside the outer template.

    Damn right about the second thing, I guess I was too tired.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Well bad news is that it doesn't work in MSVC7, which gets me madder than ever. No Partial specialation and no workaraound either
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It does work with 6 and doesn't with 7?
    And 7 hasn't got partial specialisation either?
    OMG, MS, what are you doing?????????
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I have no Idea but it doesn't look good :/ I'm counting on that parksie is going to get GCC3 work in windows environment
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I dunno what the hell MS are doing, but when I get time in a few days I'll continue my efforts to get GCC running properly.

    It compiles now, but I haven't got the linker working yet.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I've got the compiler running, and it works with kedaman's code.

    Unfortunately, I haven't managed to get the linker working yet

    Nope, not a Linux emulator - it's an environment that attempts to simulate Unix but it still requires Windows for all the process management.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    But won't the linker of gcc3 (would be the GNU linker "ln") link to linux apps?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Under Cygwin, ld still produces Win32 executables.

    ld != GCC. It's a separate program, developed totally separately.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    but gcc uses ld as linker. Is there another version supplied with cygwin?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    There has to be a different compiler/linker for every architecture/OS combination.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    keda: I just noticed that you forgot the template<> for the 1 specialisation of Max_Partial
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    yeah, you are right
    PHP Code:
    template<>
    struct Max_Partial<1>{
        
    template <class A,class B>
        
    struct Max_Partial1{
            
    typedef B Max_Partial2;
        };
    }; 
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  17. #17

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Talking BREAKING NEWS

    Partial Specialisation still not available in MSVC7 but I managet to tweak it to work again, you can now safely emulate PS again
    BTW the IDE that comes with VS.NET is truly amazing
    PHP Code:
    #include <iostream>
    using namespace std;
    template <class A,class Bstruct Selector_Partial_Outher{
        
    template<int Cstruct Selector_Partial_Inner{
            
    typedef B P;
            };
        
    template<>struct Selector_Partial_Inner<0>{
            
    typedef A P;
            };
        };

    template <class A,class B,int Cstruct Selector{
        
    typedef Selector_Partial_Outher<A,B>::Selector_Partial_Inner<C>::P P;
        };

    struct A{static const int message=1;};//YES YOU CAN DO IT NOW IN MSVC BUT ONLY WITH CONST INTEGRAL MEMBERS
    struct B{static const int message=2;};

    int main(){
        
    cout<<Selector<A,B,0>::P::message<<endl;
        
    cout<<Selector<A,B,1>::P::message<<endl;
        } 
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width