원문 출처 : http://www.openoffice.org/servlets/ReadMsg?msgId=930121&listName=dev


제목 [dev] A Static Assert implementation
표준 보기 메시지 원본 데이터 출력
Date: Tue, 11 Nov 2003 12:10:11 +0000
From: Caolan McNamara <Caolan.McNamara@Sun.COM>
Content-type: text/plain
Subject: [dev] A Static Assert implementation

We've a number of assert macros which could more profitably be picked up during compile time rather than at run time. Here's an implementation of a staticassert that I've been using successfully in the ww8 filter implemented as sw/source/filter/ww8/inc/staticassert.hxx for some time. An example usage is

StaticAssert((sizeof(stiName) / sizeof(stiName[0])) == 77,
    WrongSizeOfArray);
The constraints are that the error message is a plausible class name, i.e. no spaces and that the test can be done at compile time, e.g. array size checking etc. Perhaps this would be useful moved elsewhere as an office wide construct, instead of just for the ww8 filter.

/*
 Lifted direct from:
 Modern C++ Design: Generic Programming and Design Patterns Applied
 Section 2.1
 by Andrei Alexandrescu
*/
namespace ww
{
    template<bool> class compile_time_check
    {
    public:
        compile_time_check(...) {}
    };

    template<> class compile_time_check<false>
    {
    };
}

    /*
    Similiar to assert, StaticAssert is only in operation when NDEBUG
    is not defined. It will test its first argument at compile time 
    and on failure report the error message of the second argument,
    which must be a valid c++ classname. i.e. no spaces, punctuation 
    or reserved keywords.
    */
#ifndef NDEBUG
#   define StaticAssert(test, errormsg)                         \
    do {                                                        \
        struct ERROR_##errormsg {};                             \
        typedef ww::compile_time_check< (test) != 0 > tmplimpl; \
        tmplimpl aTemp = tmplimpl(ERROR_##errormsg());          \
        sizeof(aTemp);                                          \
    } while (0)
#else
#   define StaticAssert(test, errormsg)                         \
    do {} while (0)
#endif

C.
-- 
Caolan McNamara <Caolan.McNamara@Sun.COM>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@openoffice.org
For additional commands, e-mail: dev-help@openoffice.org

+ Recent posts