[Bug c++/101906] New: Constant evaluation failure in concepts

de34 at live dot cn gcc-bugzilla@gcc.gnu.org
Sat Aug 14 05:47:39 GMT 2021


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101906

            Bug ID: 101906
           Summary: Constant evaluation failure in concepts
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: de34 at live dot cn
  Target Milestone: ---

Following code fails to compile when the macro MAYBE_CWG1581 is not defined.

#include <bit>
#include <cstddef>

namespace test {
template<int> using voidify = void;

template<std::size_t N>
struct obj_rep { unsigned char value[N]; };

template<class T>
concept constant_value_initializable =
    requires { typename voidify<(std::bit_cast<obj_rep<sizeof(T)>>(T()), 0)>;
};
}

struct foo {
    int x;
};

struct bar {
    int x = -1;
};

#ifdef MAYBE_CWG1581
using my_void =
test::voidify<(std::bit_cast<test::obj_rep<sizeof(bar)>>(bar()), 0)>;
#endif

static_assert(test::constant_value_initializable<int>);
static_assert(test::constant_value_initializable<foo>);
static_assert(test::constant_value_initializable<bar>); // It's buggy
static_assert(!test::constant_value_initializable<int*>);
// Correct: std::bit_cast doesn't perform constant evaluation when involving
pointers

int main()
{
}

The godbolt link: https://gcc.godbolt.org/z/6dscsaGTM

It seems that currently gcc fails to generate the definition of
std::bit_cast<SomeType> when determining if the concept is satisfied, while it
can generate such definition when a type alias declaration is encountered.

Maybe related to WG21 P0859R0/CWG1581.


More information about the Gcc-bugs mailing list