Files
gcc-reflection/gcc/testsuite/g++.dg/cpp1y/constexpr-base1.C
Patrick Palka 0120cd9382 c++: constexpr pointer indirection with negative offset [PR100209]
During constexpr evaluation, a base-to-derived conversion may yield an
expression like (Derived*)(&D.2217.D.2106 p+ -4) where D.2217 is the
derived object and D.2106 is the base.  But cxx_fold_indirect_ref
doesn't know how to resolve an INDIRECT_REF thereof to just D.2217,
because it doesn't handle POINTER_PLUS_EXPR of a COMPONENT_REF with
negative offset well: when the offset N is positive, it knows that
'&x p+ N' is equivalent to '&x.f p+ (N - bytepos(f))', but it doesn't
know about the reverse transformation, that '&x.f p+ N' is equivalent
to '&x p+ (N + bytepos(f))' when N is negative, which is important for
resolving such base-to-derived conversions and for accessing subobjects
backwards.  This patch teaches cxx_fold_indirect_ref this reverse
transformation.

gcc/cp/ChangeLog:

	PR c++/100209
	* constexpr.c (cxx_fold_indirect_ref): Try to canonicalize the
	object/offset pair for a POINTER_PLUS_EXPR of a COMPONENT_REF
	with a negative offset into one whose offset is nonnegative
	before calling cxx_fold_indirect_ref_1.

gcc/testsuite/ChangeLog:

	PR c++/100209
	* g++.dg/cpp1y/constexpr-base1.C: New test.
	* g++.dg/cpp1y/constexpr-ptrsub1.C: New test.
2021-04-26 17:30:39 -04:00

29 lines
595 B
C

// PR c++/100209
// { dg-do compile { target c++14 } }
template<typename Derived>
struct __a_t
{
unsigned char A = 0;
constexpr Derived & SetA(const unsigned char & value) {
A = value;
return *static_cast<Derived *>(this);
}
};
template<typename Derived>
struct __b_t
{
unsigned char B = 0;
constexpr Derived & SetB(const unsigned char & value) {
B = value;
return *static_cast<Derived *>(this);
}
};
struct __ab_t : __a_t<__ab_t>, __b_t<__ab_t> { };
constexpr auto AB = __ab_t().SetA(100).SetB(10);
static_assert(AB.A == 100, "");
static_assert(AB.B == 10, "");