#include <type_traits>
#define IS_CONSTEXPR(...) std::integral_constant<bool, __builtin_constant_p((__VA_ARGS__, 0))>::value
struct S
{
int i = 0;
constexpr S & assign_int(int j)
{
i = j;
return *this;
}
};
inline constexpr auto s = S{}.assign_int(3); // works
static_assert(IS_CONSTEXPR(S{}.assign_int(3))); // fails in clang, passes in GCC
The macro IS_CONSTEXPR can check whether a wide variety of expressions are well-formed in a const context. I know that __builtin_constant_p is non-standard, but based on the documentation, I assume that clang's implementation is supposed to behave like GCC's. In this case, it does not.
I don't know whether this is just a deficiency of the builtin, or whether this also means clang is less likely to evaluate such expressions at compile time (when not required).
The macro
IS_CONSTEXPRcan check whether a wide variety of expressions are well-formed in a const context. I know that__builtin_constant_pis non-standard, but based on the documentation, I assume that clang's implementation is supposed to behave like GCC's. In this case, it does not.I don't know whether this is just a deficiency of the builtin, or whether this also means clang is less likely to evaluate such expressions at compile time (when not required).