[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 12:53:34 PDT 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rGcda093681bad: Update coding standards for constexpr if statements; NFC (authored by aaron.ballman).

Changed prior to commit:
  https://reviews.llvm.org/D132232?vs=453993&id=454085#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D132232/new/

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,24 @@
 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 T>
+  static constexpr bool VarTempl = true;
+
+  template<typename T>
+  int func() {
+    if constexpr (VarTempl<T>)
+      return 1;
+    else
+      static_assert(!VarTempl<T>);
+  }
+
 Turn Predicate Loops into Predicate Functions
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132232.454085.patch
Type: text/x-patch
Size: 888 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220819/71919500/attachment.bin>


More information about the cfe-commits mailing list