Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Query regd. "Modern C++ Design" program.

Reply
Thread Tools

Query regd. "Modern C++ Design" program.

 
 
frame
Guest
Posts: n/a
 
      12-11-2006
Hi,

I am trying to compile the following program, whose fragments are
presented in Section 2.1: "Compile-Time Assertions" of Chapter 2:
"Techniques" of "Modern C++ Design" by Andrei Alexandrescu, but
couldn't;

////////////////////////////////////////////////////////////////////////////////////////////////////////
template<bool> struct CompileTimeChecker
{
CompileTimeChecker(...);
};
template<> struct CompileTimeChecker<false> { };
#define STATIC_CHECK(expr, msg) \
{\
class ERROR_##msg {}; \
(void)sizeof(CompileTimeChecker<(expr) != 0>((ERROR_##msg())));\
}
template <class To, class From>
To safe_reinterpret_cast(From from)
{
STATIC_CHECK(sizeof(From) <= sizeof(To),
Destination_Type_Too_Narrow);
return reinterpret_cast<To>(from);
}
int main(int argc, char* argv[]){
void* aptr = 0;
char c = safe_reinterpret_cast<char>(aptr);
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////

As per the book, an expected result should be a compile-time error
occurence due to the "STATIC_CHECK" macro. But this hasn't been the
case with a couple of compilers (gcc, Comeau), I tried to compile this
program. I find both compiler's normal "reinterpret_cast" performing
the functionality of "safe_reinterpret_cast". Apart from these, I am
also getting an error "invalid application of `sizeof' to a function
type" for the statement "(void)sizeof(CompileTimeChecker<(expr) !=
0>((ERROR_##msg())));".

Can somebody please clarify whether anything is amiss with the above
program?

Thanks in advance!

 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      12-11-2006

frame wrote:
> Hi,
>
> I am trying to compile the following program, whose fragments are
> presented in Section 2.1: "Compile-Time Assertions" of Chapter 2:
> "Techniques" of "Modern C++ Design" by Andrei Alexandrescu, but
> couldn't;
>
> ////////////////////////////////////////////////////////////////////////////////////////////////////////
> template<bool> struct CompileTimeChecker
> {
> CompileTimeChecker(...);
> };
> template<> struct CompileTimeChecker<false> { };
> #define STATIC_CHECK(expr, msg) \
> {\
> class ERROR_##msg {}; \
> (void)sizeof(CompileTimeChecker<(expr) != 0>((ERROR_##msg())));\
> }
> template <class To, class From>
> To safe_reinterpret_cast(From from)
> {
> STATIC_CHECK(sizeof(From) <= sizeof(To),
> Destination_Type_Too_Narrow);
> return reinterpret_cast<To>(from);
> }
> int main(int argc, char* argv[]){
> void* aptr = 0;
> char c = safe_reinterpret_cast<char>(aptr);
> return 0;
> }
> ////////////////////////////////////////////////////////////////////////////////////////////////////////
>
> As per the book, an expected result should be a compile-time error
> occurence due to the "STATIC_CHECK" macro. But this hasn't been the
> case with a couple of compilers (gcc, Comeau), I tried to compile this
> program. I find both compiler's normal "reinterpret_cast" performing
> the functionality of "safe_reinterpret_cast". Apart from these, I am
> also getting an error "invalid application of `sizeof' to a function
> type" for the statement "(void)sizeof(CompileTimeChecker<(expr) !=
> 0>((ERROR_##msg())));".
>
> Can somebody please clarify whether anything is amiss with the above
> program?
>
> Thanks in advance!


There is no such thing as a "safe" reinterpret_cast except by a
conversion ctor ( hint!!! ), Note that you are attempting to convert a
void* to a char in main() above. Try a void* to char* maybe.

Consider the following where instances of both struct C and struct N
will end up having the exact same sizeof() due to member padding. The
above static macro would be converting such a invalid cast into a
destructive abomination.

struct C
{
int n;
char c;
};

struct N
{
int n;
int m;
};

int main()
{
C instance;
N another = reinterpret_cast< N >(instance); // error
}

In fact the compiler should (and does) generate an error: invalid
conversion from 'C' to 'N'.
IMHO, reinterpret_cast should never be used. Even to convert a pointer:

int main()
{
char c('a');
void* ptr_v = &c;
char* ptr_c = static_cast<char*>(ptr_c);
std::cout << "*ptr_c = " << *ptr_c;
std::cout << std::endl;
}

 
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
ASP.NET won't retrieve query results that depend on union query Eric Nelson ASP .Net 5 02-04-2009 10:51 PM
Trying to query the Address table data of AdventureWorks database from Query Analyzer - need help! Learner ASP .Net 1 01-30-2006 08:58 PM
Build dynamic sql query for JSTL <sql:query> Anonymous Java 0 10-13-2005 10:01 PM
xpath query query David Gordon XML 2 05-18-2005 03:33 PM
CAML Query: Multiple Query Fields Issue Jon F. ASP .Net Web Services 0 05-12-2004 08:19 PM



Advertisments