The following C++17 program should not compile, but it does: #include <iostream> int main() { auto d = 42; if constexpr (d > 0) { std::cout << "oops \n"; } } This even works inside a loop over different values of d. And I found it trying this: if constexpr (auto obj = 42; obj == 0) { //... } which should need a const/constexpr in the if initialization. Reported as recommended by Jonathan Wakely. Might also be a problem in 7.x
tree finish_if_stmt_cond (tree cond, tree if_stmt) { cond = maybe_convert_cond (cond); if (IF_STMT_CONSTEXPR_P (if_stmt) && is_constant_expression (cond) && !value_dependent_expression_p (cond)) { cond = instantiate_non_dependent_expr (cond); cond = cxx_constant_value (cond, NULL_TREE); } So, if is_constant_expression is true, we do the right thing. There is just no diagnostics if cond fails the is_constant_expression check and isn't value dependent. We actually treat that if constexpr like normal if, with both then and else evaluated.
Author: jason Date: Mon Dec 4 22:52:07 2017 New Revision: 255390 URL: https://gcc.gnu.org/viewcvs?rev=255390&root=gcc&view=rev Log: PR c++/83273 - constexpr if allows non-constant condition * semantics.c (finish_if_stmt_cond): Use require_constant_expression rather than is_constant_expression. * constexpr.c (potential_constant_expression_1) [LAMBDA_EXPR]: Allow in C++17. Added: trunk/gcc/testsuite/g++.dg/cpp1z/constexpr-if13.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/constexpr.c trunk/gcc/cp/semantics.c trunk/gcc/testsuite/g++.dg/cpp1z/constexpr-if12.C
Fixed.
.