recursive dereferencing library

Discussion in 'C++' started by sebastian, Feb 20, 2008.

  1. sebastian

    sebastian Guest

    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?
     
    sebastian, Feb 20, 2008
    #1
    1. Advertising

  2. sebastian

    Guest

    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:

    http://www.comeaucomputing.com/tryitout/

    Jason

    "sebastian" <> wrote in message
    news:
    ...
    >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?
     
    , Feb 20, 2008
    #2
    1. Advertising

  3. 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.
     
    Gianni Mariani, Feb 20, 2008
    #3
  4. sebastian

    sebastian Guest

    // 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.hpp"

    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_specialization_example.cpp

    #include <iostream>
    #include <memory>
    #include "extract.hpp"

    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;
    }
     
    sebastian, Feb 21, 2008
    #4
  5. sebastian wrote:
    > // extract.hpp


    .... what problem are you trying to solve ?

    It's very clever but I'm not sure it's really needed.
     
    Gianni Mariani, Feb 21, 2008
    #5
  6. sebastian

    sebastian Guest


    >> ... 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.
     
    sebastian, Feb 22, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe McGuckin

    Dereferencing ?

    Joe McGuckin, Nov 28, 2003, in forum: Perl
    Replies:
    1
    Views:
    515
    Gunnar Hjalmarsson
    Nov 28, 2003
  2. J. Campbell
    Replies:
    4
    Views:
    1,189
    Simon G Best
    Jul 17, 2003
  3. Larry Lindsey
    Replies:
    1
    Views:
    437
    Larry Lindsey
    Sep 24, 2003
  4. n00m
    Replies:
    12
    Views:
    1,133
  5. vamsi
    Replies:
    21
    Views:
    2,135
    Keith Thompson
    Mar 9, 2009
Loading...

Share This Page