强制编译时constexpr

Tho*_*ing 8 c++ constexpr c++11

在C++ 11中,我们得到constexpr:

constexpr int foo (int x) {
    return x + 1;
}
Run Code Online (Sandbox Code Playgroud)

是否可以使用编译时错误foo的动态值进行调用x?也就是说,我想创建一个foo只能传递constexpr参数的东西.

Ker*_* SB 9

用元函数替换它:

template <int x> struct foo { static constexpr int value = x + 1; };
Run Code Online (Sandbox Code Playgroud)

用法:

foo<12>::value
Run Code Online (Sandbox Code Playgroud)