Results 1 to 5 of 5

Thread: objects as template parameters

  1. #1

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

    Smile objects as template parameters

    PHP Code:
    struct asdf{int z;};
    template <asdfy>
    class 
    c{static const int l;};
    template <asdfy>
    const 
    int c<y>::l=y->z
    I wonder how the compiler deals with this? It sure compiles but the templatized class c has to instantiate at compile time, how on earth is it suppose to dereference the asdf pointer
    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
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    did you try to use this construct? I mean, which address do you supply as template parameter? It must be a constant (like 0xa00000), am I 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.

  3. #3

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    PHP Code:
    struct asdf{int z;asdf(int pz):z(pz){};}    x(4);

    template <asdfy>
    struct c{static const int l;};

    template <asdfy>
    const 
    int c<y>::l=y->z;

    int main(){ 

        
    cout << c<&x>::<< endl;
        
    x.z=5;
        
    cout << c<&x>::<< endl;

        return 
    0;
    }; 
    It seems like it's done at compile time, at least changing it at runtime won't affect the datatype. I am truly amazed by this
    BTW can anyone check it out with assembler listings? I don't want to rely that it executes a contructor and why not a whole bunch of functions at compile time if it actually doesnt
    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.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    My best guess:
    the address of a variable is known at compile time. Therefor it can be used to create a real struct out of your template. The created code looks like this:
    Code:
    struct asdf{
    int z;
    asdf(int pz)
       :z(pz)
       {};
    }    x(4);
    
    struct _@3479AD32h_@c {  // or whatever temporary name where the template parameter is coded inside
      static const int l;
    };
    
    // this is copied here, so it doesn't need to change later
    const int _@3479AD32h_@c::y = (asdf*)(0x3479AD32)->z;
    
    int main(){ 
    
        cout << _@3479AD32h_@c::l << endl;
        x.z=5;  // this has no effect because _@3479AD32h_@c::l is a copy of x.z, not a reference
        cout << _@3479AD32h_@c::l << endl;
    
        return 0;
    };
    (&x = 0x3479AD32)
    Is my assumption true?
    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.

  5. #5

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Okay That gives new life in partial evaluation with templates which didn't work too well in MSVC
    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