Sgn() function with c++ templates



  • behold!

    #include <memory>
    

    class Sgn {
    private:
    template<class T>
    static std::auto_ptr<Sgn> createSignObjectByNr( T n );

    public:
        virtual int getSign() const = 0;
       
        template&lt;class T&gt;
        static T sign( T n );
    

    };

    class SgnPositiv : public Sgn {
    public:
    int getSign() const {
    return( 1 );
    }
    };

    class SgnNegativ : public Sgn {
    public:
    int getSign() const {
    return( -1 );
    }
    };

    class SgnNull : public Sgn {
    public:
    int getSign() const {
    return( 0 );
    }
    };

    template<class T> std::auto_ptr<Sgn> Sgn::createSignObjectByNr( T n ) {
    std::auto_ptr<Sgn> ret( new SgnNull() );

    if( n &lt; T(0) )
        ret = std::auto_ptr&lt;Sgn&gt;( new SgnNegativ() );
    
    if( n &gt; T(0) )
        ret = std::auto_ptr&lt;Sgn&gt;( new SgnPositiv() );
    
    return( ret );
    

    }

    template<class T> T Sgn::sign( T n ) {
    std::auto_ptr<Sgn> sobj = Sgn::createSignObjectByNr<T>( n );
    return( T( sobj.get()->getSign() ) );
    }

    // test
    #include <iostream>

    int main() {
    std::cout << "-5 -> " << Sgn::sign( -5 ) << std::endl;
    std::cout << "+6 -> " << Sgn::sign( 6 ) << std::endl;
    std::cout << " 0 -> " << Sgn::sign( 0 ) << std::endl;
    }



  • Mmm... Smalltalky.



  • Wow. So... utterly unnecessary and a good abuse of templates. However, looks like learners code to figure out templates and inner template composition syntax to me, though, so unless it is actually used in production code no real WTF.



  • Not really a wtf unless in some production code. But even there you can find nice template metaprogramming things, I have seen big logics being determined at compile time using boost mpl. 

     If you want a real wtf, look at this little game... http://adrinael.net/variablehack.cpp



  • [quote user="PlasmaHH"]

    Not really a wtf unless in some production code. But even there you can find nice template metaprogramming things, I have seen big logics being determined at compile time using boost mpl. 

     If you want a real wtf, look at this little game... http://adrinael.net/variablehack.cpp

    [/quote]

    Why...? Dear Lord, WHY???
     



  • > variablehack

    Haha, that is very cool, now I simply must learn proper metaprogramming to be able to write stuff like that! And that's no more of an WTF than obfuscation contests or cramming huge amounts of (perceived) logic into 64Kb demoscene. Very cool, I'm inspired.



  • I especially like "delete new what;"  I'm going to start using it everywhere.

       int fn(int parm) {

          delete new int;

          delete [] new int [parm];

    #define old new

          delete old char;

    #define any

         delete any old int;

    #define thing double

          delete any old thing;

          return parm;

       }

     



  • Now just make you code to do something usefull :)

    "delete new what" actually have a load, it calls constructor (and then destructor) and fires off the whole engine :)



  • [quote user="UnFleshed One"]Now just make you code to do something usefull :)

    "delete new what" actually have a load, it calls constructor (and then destructor) and fires off the whole engine :)[/quote]

    Oh, well, I can improve on this then.

    Add to "what" a constructor that takes an int:

      what::what(int a) {

         // Copy of text of default constructor goes here

       }

    Then, in a different file, define a misleading function that takes a what:

        void sleepForNMilliseconds(what w) {

            // Sleep for the number of milliseconds given in the parameter

           // so that the other thread has a chance to catch up and we can

           // use its data without having to lock it.

           // TODO:  Implement this only if it turns out I need it.

       }

    Then you just have to put this somewhere where it gets executed:

         // Start up the other thread ...

         ...  

         // Give the other thread some time to work

         sleepForNMilliseconds(500);

        // Forcibly shut down the other thread

         ...

    A"what" will be constructed from the int to pass to sleepForNMilliseconds (and destroyed soon after).  (This is why most constructors that take only one parameter should be marked "explicit".)  So the code that does all the work runs even though it never appears to be called at all!

     



  • Hehe, that is evil enough, allright. You can insert any code with good chances nobody noticing. Perfect for making pranks on your coleagues :). 



  • [quote user="newfweiler"]

    [quote user="UnFleshed One"]Now just make you code to do something usefull :)

    "delete new what" actually have a load, it calls constructor (and then destructor) and fires off the whole engine :)[/quote]

    Oh, well, I can improve on this then.

    Add to "what" a constructor that takes an int:

      what::what(int a) {

         // Copy of text of default constructor goes here

       }

    Then, in a different file, define a misleading function that takes a what:

        void sleepForNMilliseconds(what w) {

            // Sleep for the number of milliseconds given in the parameter

           // so that the other thread has a chance to catch up and we can

           // use its data without having to lock it.

           // TODO:  Implement this only if it turns out I need it.

       }

    Then you just have to put this somewhere where it gets executed:

         // Start up the other thread ...

         ...  

         // Give the other thread some time to work

         sleepForNMilliseconds(500);

        // Forcibly shut down the other thread

         ...

    A"what" will be constructed from the int to pass to sleepForNMilliseconds (and destroyed soon after).  (This is why most constructors that take only one parameter should be marked "explicit".)  So the code that does all the work runs even though it never appears to be called at all!

    [/quote]

    Now you just need to rename what as something like Int or integer and no one will even suspect anything when they see the sleepForNMilliseconds() function. 



  • Hey, that code was cool! I wouldn't want to maintain it, though... It sort of makes C++ look a bit more like XPL (XML Programming Language) doesn't it?

    Also, since all real code is generated at compile time, imagine how much development time is saved by not having to write that code!
     


Log in to reply