recursive dereferencing library

S

sebastian

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?
 
J

jason.cipriani

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

(e-mail address removed)...
 
G

Gianni Mariani

sebastian said:
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.
 
S

sebastian

// 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;
}
 
S

sebastian

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.

its necessity or usefulness depends on the individual, I suppose.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top