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

F

frame

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

Salt_Peter

frame said:
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;
}
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top