Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Near a solution

Reply
Thread Tools

Near a solution

 
 
Tomás
Guest
Posts: n/a
 
      05-27-2006
Tomás posted:

> template<class T>
> const uchar* ByteAdrs( const T* const p )
> {
> return reinterpret_cast<const uchar*>(p);
> }
>
> template<class T>
> unsigned uchar* ByteAdrs( T* const p )
> {
> return reinterpret_cast<uchar*>(p);
> }



Building upon some sample code that Alf P. Steinbach gave me, I've come
up with the following code. It doesn't compile, but I think I'm pretty
close to a solution.

template<class T>
struct DetermineConstness {

/* Two types of differing size: */
struct SizeRepresentModifiable { char c; };
struct SizeRepresentConst { double array[50]; };

static T &ReturnRef(void);

static SizeRepresentModifiable TakesRef(T &);

static SizeRepresentConst TakesRef(const T &);

static bool const is_const =
sizeof( TakesRef( ReturnRef() ) )
== sizeof( SizeRepresentConst );
};

template<bool is_const, class T>
struct ConstnessVersion
{
typedef T Type;
};

template<class T>
struct ConstnessVersion<true, T>
{
typedef T const Type;
};


template<class T>
typename ConstnessVersion<DetermineConstness<T>::is_const, char>::Type
*ByteAdrs( T * const p )
{
return
reinterpret_cast
<typename ConstnessVersion<DetermineConstness<T>::is_const,
char>::Type *>
(p);
}

#include <string>
using std::string;

int main()
{
string str;
string const cstr;

const char *p1 = ByteAdrs(&str); /* No problem */
const char *p2 = ByteAdrs(&cstr); /* No problem */

char *p3 = ByteAdrs(&str); /* No problem */
//char *p4 = ByteAdrs(&cstr); /* ERROR: const clash */
}


Any ideas? Once I get this to work I'll start back on trying to write a
proper "implicit_cast".


-Tomás
 
Reply With Quote
 
 
 
 
kwikius
Guest
Posts: n/a
 
      05-28-2006

Tomás wrote:


> Any ideas? Once I get this to work I'll start back on trying to write a
> proper "implicit_cast".


What is implicit_cast's purpose? FWIW there is a (currently
undocumented but hopefully documented in next release) function named
implicit_cast in the boost distro. Header is <boost/implicit_cast.hpp>

regards
Andy Little

 
Reply With Quote
 
 
 
 
Tomás
Guest
Posts: n/a
 
      05-28-2006
kwikius posted:

> FWIW there is a (currently
> undocumented but hopefully documented in next release) function named
> implicit_cast in the boost distro. Header is <boost/implicit_cast.hpp>



I downloaded it just there and tried it out... it works perfectly! It's a
stroke of genius! I had to look through the source code to figure out how
it works, but unforunately it's flooded with macros so it would take quite
a while.

It seems that the "identity" template is the magic behind it.


-Tomás
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ solution for K & R(2nd Ed) Ex.6-4 - better solution needed subramanian100in@yahoo.com, India C++ 17 10-01-2007 09:00 AM
Solution file not in the solution folder =?Utf-8?B?Y2FzaGRlc2ttYWM=?= ASP .Net 2 09-12-2006 11:04 AM
A Solution using Tasks Re: [Stackless] Suggestion for a Solution ? Andrew Francis Python 0 06-28-2006 06:05 PM



Advertisments