admin管理员组文章数量:1379411
Consider the following code:
enum { a = 1, b = 2 - a};
void foo() {
if constexpr(a == 1) { }
else { static_assert(b != 1, "fail :-("); }
}
naively, one might expect the static_assert()
to never be triggered, since the if constexpr
condition holds. But - it is triggered (GodBolt).
What C++ mechanism should I use instead, to ensure failure only when the condition of an if statement evaluates to false?
Consider the following code:
enum { a = 1, b = 2 - a};
void foo() {
if constexpr(a == 1) { }
else { static_assert(b != 1, "fail :-("); }
}
naively, one might expect the static_assert()
to never be triggered, since the if constexpr
condition holds. But - it is triggered (GodBolt).
What C++ mechanism should I use instead, to ensure failure only when the condition of an if statement evaluates to false?
Share Improve this question edited Mar 18 at 20:53 Bergi 666k161 gold badges1k silver badges1.5k bronze badges asked Mar 18 at 15:31 einpoklumeinpoklum 133k80 gold badges421 silver badges864 bronze badges 4 |1 Answer
Reset to default 1What C++ mechanism should I use instead, to ensure failure only when the condition of an if statement evaluates to false?
According to cppreference
Outside a template, a discarded statement is fully checked. if constexpr is not a substitute for the #if preprocessing directive:
so I suppose you can make your foo()
function a template one
template <int = 0>
void foo() {
if constexpr(a == 1) { }
else { static_assert(b != 1, "Fail"); }
}
--- EDIT ---
The OP asks
it's quite problematic to change the function definition just for an assertion somewhere... can't I use some kind of templated gadget instead?
I don't see a way through a type trait.
But if you can use at least C++20, so template lambdas, you can wrap your code inside a template lambda defined inside the foo()
function. And call the lambda.
I mean something as follows
enum { a = 1, b = 2 - a};
void foo() {
auto l = []<int = 0>{
if constexpr (a == 1) {
}
else {
static_assert(b != 1, "fail :-(");
}
};
l();
}
int main ()
{
foo();
}
本文标签:
版权声明:本文标题:c++ - How do I make a "static assertion" which is only checked on one branch of an if constexpr? - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744504566a2609516.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
static_assert(a == 1 || b != 1, "fail :-(");
Since a static_assert isn't scoped in this case (... because it is static and not in a template), so doesn't matter that it is in the else block. – Eljay Commented Mar 18 at 21:57a
as I write the static asseration, or whatever alternative I use. Also, DRY. – einpoklum Commented Mar 18 at 22:07if constexpr
has no meaning outside a template. It's purpose is to eliminate the branching when the template is instantiated (hence the need to have aconstexpr
condition). In your case, it's equivalent to a regularif
but restricted toconstexpr
conditions only (which is then useless). – Fareanor Commented Mar 19 at 9:41