[PATCH] D132232: Update coding standards for constexpr if statements; NFC
Aaron Ballman via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 19 07:37:13 PDT 2022
aaron.ballman created this revision.
aaron.ballman added reviewers: dblaikie, rjmccall, echristo, erichkeane.
Herald added a project: All.
aaron.ballman requested review of this revision.
Herald added a project: LLVM.
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.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D132232
Files:
llvm/docs/CodingStandards.rst
Index: llvm/docs/CodingStandards.rst
===================================================================
--- llvm/docs/CodingStandards.rst
+++ llvm/docs/CodingStandards.rst
@@ -1026,6 +1026,21 @@
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 <typename Ty>
+ static int foo(Ty T) {
+ if constexpr (std::is_integral_v<Ty>)
+ return 10 + T;
+ else
+ return 100;
+ }
+
Turn Predicate Loops into Predicate Functions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132232.453993.patch
Type: text/x-patch
Size: 831 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220819/36318504/attachment-0001.bin>
More information about the cfe-commits
mailing list