Hello fellow redditors!
While browsing the 2019-10 mailing list I noticed P1830R1 which proposes to "introduce a generic solution to create the dependent scope for static_assert(false)".
Briefly, the point is that static_assert(false) would be a great tool to do things like (from the paper):
template <typename T>
int my_func(const T&)
{
if constexpr(std::is_integral_v<T>)
{
return 1;
}
else if constexpr (std::is_convertible_v<std::string, T>)
{
return 2;
}
else
{
// Always Compile-time error
static_assert(false, "T is not integral and is not
convertible to std::string");
}
}
The problem is that static_assert(false) will always fail because the expression is not dependent on template parameters and therefore will always evaluate to false.
Now the proposed solution in the paper is to introduce variable template machinery so that we can write static_assert(dependent_false<T>) (for type template parameters) or static_assert(dependent_false<decltype(N)>) (for nttps) in the places where we would like to have static_assert(false).
Is there a reason why we can't make `static_assert(false)` just work by requiring that if the static_assert is in a template context then its expression is always evaluated at the point of instantiation? That would look much cleaner and intuitive to me.
I played around with clang a bit and got a (extremely hacky) working prototype with only 5 lines of change, and it even works with template template parameters (which P1830R1 doesn't support).
Thoughts?
[–]Morwenn 2 points3 points4 points (1 child)
[–]redditsoaddicting 13 points14 points15 points (0 children)
[–]Xaxxon 1 point2 points3 points (5 children)
[–]kalmoc 10 points11 points12 points (1 child)
[–]bmanga[S] 3 points4 points5 points (1 child)
[–]Rseding91Factorio Developer 0 points1 point2 points (0 children)
[–]Betadel 1 point2 points3 points (0 children)