#172 LOKI_STATIC_CHECK not working in namespace scope

closed-fixed
None
5
2011-09-09
2010-07-01
zhnmju123
No

Hi,

the way Loki enables compile time checks is very small and elegant. However they are not working in namespace scope:

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

What about creating an unnamed class instead? This would enable to use static checks in function AND namespace scope. Like:

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

Best regards, ZenJu

Discussion

  • zhnmju123

    zhnmju123 - 2010-07-01

    Okay, just found part of the answer myself. While this approach works on Borland C++, it doesn't on GCC,

    Nevertheless it would be nice if static check would support namespace and possibly class scope. Just like Boost static assert, but without the "mess" of additional header and compiler dependencies, i.e. in the clean "loki style".

     
  • Richard Sposato

    Richard Sposato - 2010-07-02

    Hi ZenJu!

    Thanks for notifying us about the static checking problem. Since your comment shows you already know about the hassles of getting this to work on GCC, I'll just admit that we strive to make Loki compile cleanly on at least Visual Studio and GCC. So working out a solution to the static check problem that works on Borland, Visual Studio, and GCC is going to take some effort.

    If you come up with a solution that works on all 3, then by all means, let us know!

    Cheers,

    Rich

     
  • zhnmju123

    zhnmju123 - 2010-07-02

    Alright, I have assembled a new proposal. I've merged Loki::static_check and boost::static_assert into a new version that hopefully provides the best of both worlds: It's small and works for function, namespace and class-scope. I've tested on Visual C++ 2010, GCC 4.5.0-1(Windows MinGW) and GCC 4.4.3(Ubuntu)

    It partially relies on macro-magic, so I'm crossing fingers that it works on other compilers as well...

    #ifndef LOKI_STATIC_CHECK_INC_
    #define LOKI_STATIC_CHECK_INC_

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

    #define LOKI_CONCAT( X, Y ) LOKI_CONCAT_SUB( X, Y )
    #define LOKI_CONCAT_SUB( X, Y ) X##Y

    #define LOKI_STATIC_CHECK(expr, msg) \ enum { LOKI_CONCAT(ERROR_##msg, __LINE__) = sizeof(Loki::CompileTimeError<expr != 0 >) }

    #endif // end file guardian

     
  • zhnmju123

    zhnmju123 - 2010-07-02

    Just tested with Borland C++ Compiler 5.5. Works. It looks promising :)

     
  • Richard Sposato

    Richard Sposato - 2010-07-03
    • assigned_to: nobody --> rich_sposato
     
  • Richard Sposato

    Richard Sposato - 2010-07-03

    Hi ZenJu!

    So you have a solution that compiles and works on 4 compilers? Excellent! And a simple solution at that? Even better! Let me try a few tests with it. If it works, I'll take it from there and give you credit for the fix!

    Thanks!

    Rich

     
  • Richard Sposato

    Richard Sposato - 2011-09-09

    Fixed in revision 1099.

    Fixed by using unnamed enum as shown in ZenJu's comment. Thanks!

     
  • Richard Sposato

    Richard Sposato - 2011-09-09
    • status: open --> closed-fixed
     

Log in to post a comment.