[PATCH] D132232: Update coding standards for constexpr if statements; NFC

Erich Keane via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 19 07:47:27 PDT 2022


erichkeane added a comment.

Agreed.  For other reviewers, note that this is the compelling example:

  template<typename T>
  static constexpr bool VarTempl = true;
  
  template<typename T>
  int func() {
    if constexpr (VarTempl<T>)
      return 1;
    
    static_assert(!VarTempl<T>);
  }
  
  void use() {
      func<int>();
  }

The static-assert would fail, since it isn't a discarded statement.

  template<typename T>
  static constexpr bool VarTempl = true;
  
  template<typename T>
  int func() {
    if constexpr (VarTempl<T>)
      return 1;
    else 
    static_assert(!VarTempl<T>);
  }
  
  void use() {
      func<int>();
  }

But in THIS case the static-assert is discarded. That is, the 'else' in a if-constexpr isn't required to be anything other than lexically valid, so dependent lookup doesn't need to happen, instantiations aren't required, etc.

Therefore there is EXTREME value to having an 'else' there after a return.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D132232



More information about the cfe-commits mailing list