有可能写出类似"f <N>只能在N> 0时编译"的东西吗?

ggr*_*grr 2 c++ templates

例如,我可以在编译时检查N是否> 0,如下所示:

#include <stdio.h>
template<int N>
struct Is{
    enum{Positive=N>0?1:0};
};

template<>
    struct Is<0>{
};

int main(){
    printf("%d\n",Is<3>::Positive);
    printf("%d\n",Is<-3>::Positive);
    return 0;
};
Run Code Online (Sandbox Code Playgroud)

哪个过滤器0强制Is <0> :: Positive无法编译,但有没有任何方法(例如:模板,宏....)强制Is <N> ::正当N不是> 0时无法编译?

DrP*_*zza 6

static_assert(N > 0, "N must be greater than zero")