I wanted to do a static_assert inside my template on an array created from a parameter pack.
Consider the following code:
template<typename... Args>
auto constexpr CreateArrConst(Args&&... args)
{
std::array arr
{
args...
};
return arr;
}
template<typename... Args>
auto constexpr CreateArrConst_NotWorking(Args&&... args)
{
constexpr std::array arr
{
args...
};
static_assert(arr.back() == 4);
return arr;
}
int main()
{
static_assert(CreateArrConst(4).back() == 4);
// uncomment this to reproduce compile error
// static_assert(CreateArrConst_NotWorking(4).back() == 4);
return 0;
}
The first template can create a constexpr which I can use at compile time to check inside main. However, once I try to create the same array (this time as constexpr) inside the template in CreateArrConst_NotWorking I get:
error: ‘args#0’ is not a constant expression
Godbolt link to reproduce: https://godbolt.org/z/YjGq76e5j
Does anyone know why I cannot create the constexpr inside the template?
argsisn't a constant expression, maybe that's why.