Skip to content

__builtin_constant_p fails/behaves differently from GCC #58078

Description

@h-2
#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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++clang:frontendLanguage frontend issues, e.g. anything involving "Sema"confirmedVerified by a second party

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions