From cda093681bad901e64d0f6898d15121ab49cb074 Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Fri, 19 Aug 2022 15:51:53 -0400 Subject: [PATCH] Update coding standards for constexpr if statements; NFC We currently suggest that users not use an else clause after a return statement in a prior if branch. e.g., if (foo) return 1; else // Should remove this else clause return 10; however, this suggestion is incorrect for a constexpr if statement because one of the two branches will be a discarded statement and thus can impact template instantiation behavior. This updates the coding standard to make it clear that it's okay to have a return after an else in a constexpr if statement. I think this is an NFC change to the intent of the rule, which is why I've not started an RFC for the changes. Differential Revision: https://reviews.llvm.org/D132232 --- llvm/docs/CodingStandards.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/llvm/docs/CodingStandards.rst b/llvm/docs/CodingStandards.rst index fd5bf25..0f30594 100644 --- a/llvm/docs/CodingStandards.rst +++ b/llvm/docs/CodingStandards.rst @@ -1026,6 +1026,24 @@ Or better yet (in this case) as: The idea is to reduce indentation and the amount of code you have to keep track of when reading the code. +Note: this advice does not apply to a ``constexpr if`` statement. The +substatement of the ``else`` clause may be a discarded statement, so removing +the ``else`` can cause unexpected template instantiations. Thus, the following +example is correct: + +.. code-block:: c++ + + template + static constexpr bool VarTempl = true; + + template + int func() { + if constexpr (VarTempl) + return 1; + else + static_assert(!VarTempl); + } + Turn Predicate Loops into Predicate Functions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- 2.7.4