Is it possible to use a static_assert
in conjunction with a template alias? I understand how to use SFINAE with a template alias, and how to use static_assert
with a struct
, but I want the static_assert
with the alias to give a cleaner error message.
I have the following use case in mind:
#include <array>
constexpr bool is_valid(int n){
return n <= 10;
}
template <int n>
struct Foo {
static_assert(is_valid(n), "This class cannot handle more than 10 dimensions");
};
template <int n>
using Bar = std::array<float,n>;
template <int n, std::enable_if_t<is_valid(n)> * unused = nullptr>
using BarSFINAE = std::array<float,n>;
int main() {
Foo<5>();
// Foo<20>(); // Triggers the compiler-time static_assert
Bar<5>();
Bar<20>(); // TODO: Should trigger a compiler-time static_assert
BarSFINAE<5>();
// BarSFINAE<20>(); // Not allowed due to SFINAE, but throws an ugly compile time message
}
The issue is essentially that an alias doesn't have a body. So I don't know where I would even put a static_assert
.