473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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

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<boolstruct 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!

Dec 11 '06 #1
1 1293

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<boolstruct 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;
}

Dec 11 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: nobody | last post by:
hello! does anybody have 1st hand experience about how to achieve the "modern" Spanish sort order (a b c ch cz d ... l ll lz m n nz ñ o ..., ie only ñ is treated as separate letter)? with...
3
by: Animesh | last post by:
Hi All, I have a lexical analyzer in flex/bison associated with a lot of custom C function calls linking to external programs which uses quite a lot of global variables and data structures. The...
2
by: NowItsWhatever | last post by:
In query DESIGN view, how do I automatically "fit" the columns in the table/field grid to the lengths of the table and field names (including any functions applied to the fields). I am not talking...
4
by: vbie | last post by:
For some reason, I cannot open an Access report in "design" mode anymore. This didn't happen before. However, I can open the design mode of the form, query, table in the same database. Please...
3
by: alan.chambers | last post by:
I am new to C++/CLI. I want to do a very simple thing. In other C++ applications, my forms have always been 'context free' in the sense that I pass them a temporary copy of the data for them to...
48
by: Tony | last post by:
How much bloat does the STL produce? Is it a good design wrt code bloat? Do implementations vary much? Tony
3
by: mnjkahn via AccessMonster.com | last post by:
I'm running Access 2003, modifying a query that has over 45 fields. When I right click on the field name in Query Design View, and then click Build, Access crashes before the Build window...
19
by: maya | last post by:
hi, so what is "modern" javascript?? the same as "DOM-scripting"? i.e., editing content (or changing appearance of content) dynamically by massaging javascript objects, html elements, etc? ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.