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

Discussion in 'C++' started by frame, Dec 11, 2006.

  1. frame

    frame Guest

    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!
     
    frame, Dec 11, 2006
    #1
    1. Advertising

  2. frame

    Salt_Peter Guest

    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;
    }
     
    Salt_Peter, Dec 11, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWFudQ==?=

    Regd---Justify option in Alignment.

    =?Utf-8?B?bWFudQ==?=, Sep 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    456
    =?Utf-8?B?bWFudQ==?=
    Sep 23, 2005
  2. Michael

    Modern C++ Design

    Michael, Nov 30, 2004, in forum: C++
    Replies:
    4
    Views:
    406
    Matthias =?ISO-8859-1?Q?K=E4ppler?=
    Nov 30, 2004
  3. frame
    Replies:
    2
    Views:
    324
    frame
    Sep 25, 2006
  4. dragoncoder
    Replies:
    4
    Views:
    326
    Gianni Mariani
    Nov 8, 2006
  5. Sunil

    Query regd. memory usage

    Sunil, May 9, 2007, in forum: Java
    Replies:
    4
    Views:
    303
    Sunil
    May 10, 2007
Loading...

Share This Page