Issue 180769
Summary requires expr not check static_cast, cause: 'integer value 2 is outside the valid range of values [0, 1] for the enumeration type'
Labels new issue
Assignees
Reporter zxlxz
    
a demo to check if a `value` can static_cast to type `E`

```cpp
template <class E, auto I>
consteval auto safe_cast() -> Option<E> {
  if constexpr (requires { static_cast<E>(I); }) {
 return static_cast<E>(I);
  }
  return {};
}
```

```cpp
template <class E, auto I>
consteval auto safe_cast() -> Option<E> {
  if constexpr (requires { sizeof(char[static_cast<E>(I)]); }) {
    return static_cast<E>(I);
  }
  return {};
}
```

test with:
```cpp
enum E1{A, B};
safe_cast(E1, 2);
```

compile with:
> clang --version 
Apple clang version 17.0.0 (clang-1700.6.3.2)
Target: arm64-apple-darwin24.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

error: integer value 2 is outside the valid range of values [0, 1] for the enumeration type 'E1' [-Wenum-constexpr-conversion]
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to