llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.24k stars 12.07k forks source link

`__attribute__((enable_if(...)))` checked before constraints #115230

Open pkasting opened 2 weeks ago

pkasting commented 2 weeks ago

When using __attribute__((enable_if(...))) with a condition that may be ill-formed, you cannot use a requires clause to constrain the function to only be considered in the well-formed cases, because apparently the enable_if condition is considered first.

The workaround is to use SFINAE instead, which is ugly.

Sample code: https://godbolt.org/z/x3MjWv1ET

IMO, enable_if should be considered after constraint satisfaction (like with SFINAE), not before. Optionally, if the condition for enable_if is ill-formed the overload could just be considered to be not enabled, but that might be a bridge too far.

llvmbot commented 2 weeks ago

@llvm/issue-subscribers-clang-frontend

Author: None (pkasting)

When using `__attribute__((enable_if(...)))` with a condition that may be ill-formed, you cannot use a `requires` clause to constrain the function to only be considered in the well-formed cases, because apparently the `enable_if` condition is considered first. The workaround is to use SFINAE instead, which is ugly. Sample code: https://godbolt.org/z/x3MjWv1ET IMO, enable_if should be considered after constraint satisfaction (like with SFINAE), not before. Optionally, if the condition for `enable_if` is ill-formed the overload could just be considered to be not enabled, but that might be a bridge too far.