Constexpr allocating temporaries in if constexpr are rejected. (original) (raw)

Clang fails to compile following while gcc allows it:

https://godbolt.org/z/KcqTdeodx

#include

// ok static_assert(!std::vector{1,2,3}.empty());

int main() { if constexpr(!std::vector{1,2,3}.empty()) { return 1; } return 0; }

Error message:

<source>:7:18: error: constexpr if condition is not a constant expression
    7 |     if constexpr(!std::vector{1,2,3}.empty()) {
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.1/../../../../include/c++/15.0.1/bits/alloc_traits.h:614:20: note: allocation performed here was not deallocated
  614 |       { return __a.allocate(__n); }
      |      

The same conditional is allowed in static_assert but not in if constexpr.