[clang] [Clang] Add __builtin_is_within_lifetime to implement P2641R4's std::is_within_lifetime (PR #91895)

Hana Dusíková via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 19 12:11:14 PDT 2024


hanickadot wrote:

I was also implementing this and run into an issue, so I looked into your implementation and you have exactly same issue as I do, following code will fail:
```c++
namespace std {
template <typename T> consteval bool is_within_lifetime(const T * ptr) noexcept {
	return __builtin_is_within_lifetime(ptr);
}
}

constexpr bool test_union(int * i) { // <-- note the constexpr here
  return std::is_within_lifetime(i);
}

static_assert([] consteval {
  union { int i; char c; } u = {.i = 10};
  return test_union(&u.i);
}());
```

with error: 
```
./builtin-is-within-lifetime-call.cpp:8:10: error: call to consteval function 'std::is_within_lifetime<int>' is not a constant expression
    8 |   return std::is_within_lifetime(i);
      |          ^
./builtin-is-within-lifetime-call.cpp:8:34: note: function parameter 'i' with unknown value cannot be used in a constant expression
    8 |   return std::is_within_lifetime(i);
      |                                  ^
./builtin-is-within-lifetime-call.cpp:7:33: note: declared here
    7 | constexpr bool test_union(int * i) { // <-- note the constexpr here
      |                                 ^
1 error generated.
```

https://github.com/llvm/llvm-project/pull/91895


More information about the cfe-commits mailing list