C++ Logo

std-proposals

Advanced search

[std-proposals] constraint that function can/shall be evaluated at compile time

From: Robin Savonen Söderholm <robinsavonensoderholm_at_[hidden]>
Date: Sun, 15 Sep 2024 10:36:15 +0200
Hi!

Another thing I found that I'd like in concepts is the notion that e.g. a
function related to a type can or shall be evaluated at compile time. For
example, this:
```c++
template <typename T>
concept has_foo = requires() {
  { T::foo() /*consteval*/ } -> is_foo_like;
};
```
This is because I may want to use the result of `foo` to distinguish what
kind of reflections I should be allowed to use on `T`, and what should be
considered a compile error. I.e. I want to use it as:
```c++
void bar(has_foo auto const& my_t) {
if constexpr(T::foo(/*possible use of consteval arguments as well*/) ==
something_else) {
  do_foo_only_stuff(my_t);
}
}
```

// Robin

Received on 2024-09-15 08:38:28