How to do static_assert with macros?

后端 未结 5 2088
温柔的废话
温柔的废话 2020-12-02 01:27

I have tried to use this suggestion to do a static assert, but I do not get a compilation error if I use it within a method of a template.

The example follows :

5条回答
  •  时光取名叫无心
    2020-12-02 02:09

    This is basically Maxim's answer with a little more convenient interface. I have taken it from here. Nice thing about it is that the use of templates prevents the user from passing a non-compile-time-constant value as the condition.

    template
    struct Static_assert_cpp98
    {
      static void apply() {static const char junk[ Is_Condition_Met ? 1 : -1 ];}
    };
    
    template<>
    struct Static_assert_cpp98
    {
      static void apply() {}
    };
    
    #define STATIC_ASSERT_CPP98(condition) Static_assert_cpp98::apply()
    

提交回复
热议问题