I am very confused by why the following code doesn't compile:
template <typename T, typename... Ts>
void TEST(T&& t, Ts&&... ts) {
if constexpr(sizeof...(ts) > 0) {
TEST(std::forward<Ts>(ts)...);
static_assert( t == 2 , "parameter must be 2");
}
}
template <typename... Ts>
void VARIADIC(Ts&&... ts) {
TEST(std::forward<Ts>(ts)...);
}
int main(int argc, char* argv[]) {
constexpr int a1 = 2;
constexpr int a2 = 2;
VARIADIC(a1, a2);
}
Basically, I am trying to do some compile-time check on the parameters passed to the function VARIADIC
. However, the compiler complained about the following:
error: non-constant condition for static assertion
static_assert( t == 2 , "parameter must be 2");
^~~~~~~~~~~~~
error: ‘t’ is not a constant expression
It is very obvious the given parameters a1
and a2
are constants, and there must be some ways to perform the check on variadic arguments.