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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits