recursive dereferencing library

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

    recursive dereferencing library

    I have a dereferencing template that I believe to be implemented
    correctly and complete, but I would like to have it checked for
    correctness to be sure. is this the right forum for such a thing?
  • jason.cipriani@gmail.com

    #2
    Re: recursive dereferencing library

    Sure, give it a whirl. At the bare minimum you'll get lots of good
    advice about undefined gotchas and other weird stuff. As far actually
    checking the syntax (not necessarily correctness of your
    implementation) , you can also try compiling your code with Comeau's
    online compiler here:



    Jason

    "sebastian" <smgarth2u@yaho o.comwrote in message
    news:
    62973aa1-2f16-490f-9a71-103387610fee...l egroups.com...
    >I have a dereferencing template that I believe to be implemented
    correctly and complete, but I would like to have it checked for
    correctness to be sure. is this the right forum for such a thing?

    Comment

    • Gianni Mariani

      #3
      Re: recursive dereferencing library

      sebastian wrote:
      I have a dereferencing template that I believe to be implemented
      correctly and complete, but I would like to have it checked for
      correctness to be sure. is this the right forum for such a thing?
      If it's a question about the proper use of C++ then it is appropriate.

      Comment

      • sebastian

        #4
        Re: recursive dereferencing library

        // extract.hpp

        #ifndef XTD_EXTRACT_HPP
        #define XTD_EXTRACT_HPP

        namespace xtd {

        /*
        template to be specialized for user defined types
        */

        template < typename Type >
        struct content
        {
        typedef Type type;
        typedef type & reference;

        static inline
        reference
        extractor( reference object )
        {
        return object;
        }
        };

        /*
        user entry point
        */

        template < typename Type >
        inline
        typename content< Type >::reference
        extract( Type & object )
        {
        return content< Type >::extractor( object );
        }

        /*
        specializations for references and pointers
        */

        template < typename Type >
        struct content< Type & >
        {
        typedef typename content< Type >::type type;
        typedef type & reference;

        static inline
        reference
        extractor( Type & object )
        {
        return extract( object );
        }
        };

        template < typename Type >
        struct content< Type * >
        {
        typedef typename content< Type >::type type;
        typedef type & reference;

        static inline
        reference
        extractor( Type * object )
        {
        return extract( *object );
        }
        };

        template < typename Type >
        struct content< Type * const >
        {
        typedef typename content< Type >::type type;
        typedef type & reference;

        static inline
        reference
        extractor( Type * const object )
        {
        return extract( *object );
        }
        };

        /*
        generic function object
        */

        struct extractor
        {
        template < typename Type >
        inline
        typename content< Type >::reference
        operator ( ) ( Type & object )
        {
        return extract( object );
        }
        };

        } // namespace xtd

        #endif // XTD_EXTRACT_HPP

        // pointer_example .cpp

        #include <iostream>
        #include "extract.hp p"

        using namespace std;
        using namespace xtd;

        int
        main( void )
        {
        int
        i = 1024,
        * p = &i,
        ** pp = &p,
        *** ppp = &pp;
        extract( ppp )++;
        cout << extract( ppp ) << endl;
        return 0;
        }

        // auto_pointer_sp ecialization_ex ample.cpp

        #include <iostream>
        #include <memory>
        #include "extract.hp p"

        namespace xtd {

        template < typename Type >
        struct content< std::auto_ptr< Type
        {
        typedef typename content< Type >::type type;
        typedef type & reference;

        static inline
        reference
        extractor( std::auto_ptr< Type & object )
        {
        return extract( *object.get( ) );
        }
        };

        template < typename Type >
        struct content< std::auto_ptr< Type const >
        {
        typedef typename content< Type >::type type;
        typedef type & reference;

        static inline
        reference
        extractor( std::auto_ptr< Type const & object )
        {
        return extract( *object.get( ) );
        }
        };

        } // namespace xtd

        using namespace std;
        using namespace xtd;

        int
        main( void )
        {
        auto_ptr< auto_ptr< int
        ap( new auto_ptr< int >( new int( 1024 ) ) );
        extract( ap )++;
        cout << extract( ap ) << endl;
        return 0;
        }

        Comment

        • Gianni Mariani

          #5
          Re: recursive dereferencing library

          sebastian wrote:
          // extract.hpp
          .... what problem are you trying to solve ?

          It's very clever but I'm not sure it's really needed.

          Comment

          • sebastian

            #6
            Re: recursive dereferencing library

            >... what problem are you trying to solve ?
            the sole purpose of the library is to simplify complex dereferencing
            accesses. also, it often allows the underlying storage of data to
            change without necessarily changing the accessor code.
            >but I'm not sure it's really needed.
            its necessity or usefulness depends on the individual, I suppose.

            Comment

            Working...