How to do static_assert with macros?

后端 未结 5 2099
温柔的废话
温柔的废话 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:14

    int foo(const int k)
    {
      STATIC_ASSERT( k > 9, error_msg );
      return k+5;
    }
    

    Static assertions only work with compile-time constant expressions.

    k is not a compile-time constant expression.

    Non-type template parameters are compile-time constant expressions during template instantiation, so you could adapt your code thus:

    template 
    int foo()
    {
      STATIC_ASSERT( K > 9, error_msg );
      return K+5;
    }
    

提交回复
热议问题