Issue with CompileTimeChecker

Help
2007-12-24
2013-04-08
  • Martin Dong

    Martin Dong - 2007-12-24

    Hi,

    First I would like to say merry christmas to all of you.

    Now I am blocked by CompileTimeChecker implementation which is different in the book "Modern C++ Design" and loki library.

    The code in page 21 in the book can't be compiled on gcc3.3.3.

    -----------------------------------------------------------------------------------
    template<bool> struct CompileTimeChecker
    {
        CompileTimeChecker(...);
    };
    template<> struct CompileTimeChecker<false> { };

    #define STATIC_CHECK(expr, msg) \ {\     class ERROR_##msg {}; \     (void)sizeof(CompileTimeChecker < ((expr)!=0) > ((ERROR_##msg())));\ }

    error msg in g++:

      ISO C++ forbids applying `sizeof' to a function type

    it seems compiler assumes "CompileTimeChecker < ((expr)!=0) > ((ERROR_##msg()))" is a function expression, but sizeof can surely accept a function. why here it fails?
    -----------------------------------------------------------------------------------

    And in loki library (0.1.6), CompileTimeChecker looks like as following:

    -----------------------------------------------------------------------------------

    namespace Loki
    {
    ////////////////////////////////////////////////////////////////////////////////
    // Helper structure for the STATIC_CHECK macro
    ////////////////////////////////////////////////////////////////////////////////

        template<int> struct CompileTimeError;
        template<> struct CompileTimeError<true> {};
    }

    ////////////////////////////////////////////////////////////////////////////////
    // macro STATIC_CHECK
    // Invocation: STATIC_CHECK(expr, id)
    // where:
    // expr is a compile-time integral or pointer expression
    // id is a C++ identifier that does not need to be defined
    // If expr is zero, id will appear in a compile-time error message.
    ////////////////////////////////////////////////////////////////////////////////

    #define STATIC_CHECK(expr, msg) \     { Loki::CompileTimeError<((expr) != 0)> ERROR_##msg; (void)ERROR_##msg; }

    -----------------------------------------------------------------------------------

    loki library can work as designed, but I'm not clear about the above definiton of macro STATIC_CHECK,
       Loki::CompileTimeError<((expr) != 0)> ERROR_##msg; (void)ERROR_##msg;
    Could someone help explain what does above expression construct?

    Moreover, compared with loki library, the core implementation in the book differs from the loki library in following aspect:

    In the book, the generalized template defines a construct which can accept any parameter, and specializes the template with "false";

    On the contrary, in loki library, the generalized template is empty, and specializes the template with "true".

    Why there's a difference? Does anyone meet above problem?

    Thanks and Best Regards,
    Martin

     
    • Peter Kuemmel

      Peter Kuemmel - 2008-07-15

      gcc > 3.4

       

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks