How can I make compile time assertions without C++11

前端 未结 3 2016
囚心锁ツ
囚心锁ツ 2021-02-06 00:10

In a job interview, I was asked to write a metafunction that determined whether a type was a pointer. This is what I presented:

template 
struc         


        
相关标签:
3条回答
  • 2021-02-06 00:36

    There are different approaches, a common one trying to typedef an invalid type:

    #define static_assert(condition) \
            typedef char assert ## __LINE__ [((condition)?1:-1)]
    

    This can be used in mostly any context and will trip the compiler if the condition is false, since it would try to typedef an invalid type (array of negative number of elements). It can be used in different contexts:

    // namespace level:
    static_assert(sizeof(int)==4);
    struct type {
       // class level:
       static_assert(sizeof(int)==4);
       void f() {
           // function level
           static_assert(sizeof(int)==4);
       }
    };
    
    0 讨论(0)
  • 2021-02-06 00:41

    I would use BOOST_STATIC_ASSERT. You can look at the code: boost/static_assert.hpp.

    Here's a very simplified version, just to give you an idea:

    #define JOIN(X, Y) DO_JOIN(X, Y)
    #define DO_JOIN(X, Y) X ## Y
    
    template<bool cond>
    struct Static_assert_helper;  // incomplete type
    
    template<>
    struct Static_assert_helper<true> {
        typedef int Type;
    };
    
    #define STATIC_ASSERT(cond) \
        typedef Static_assert_helper<(cond)>::Type JOIN(Static_assert_typedef_, __LINE__)
    

    It can be used in many places (see the documentation for examples).

    (Boost's implementation is more complete, with e.g. a sizeof and an intermediate struct, to give a better error message and be portable on a wide range of compilers.)

    0 讨论(0)
  • 2021-02-06 00:43

    In your case

    template <bool> struct assert;
    template <> struct assert<true> {};
    

    would have solved the problem:

    assert<!is_pointer<char>::value>();     // valid
    assert<is_pointer<char *>::value>();    // valid
    
    assert<is_pointer<char>::value>();      // compilation error:
                                            // use of incomplete class
    
    0 讨论(0)
提交回复
热议问题