From: Toon Claes Date: Fri, 03 Apr 2026 11:13:11 GMT Subject: Re: [PATCH v2 08/12] skip_prefix(): check const match between in and out params Message-ID: <87h5ps5mbc.fsf@toon--20250203-5JQV3.mail-host-address-is-not-set> In-Reply-To: <20260402041507.GH3501239@coredump.intra.peff.net> Jeff King writes: > There are a lot of ways to implement the "fails" part. You can use > __builtin_types_compatible_p() to check, and then either our > BUILD_ASSERT macros or _Static_assert to fail. But that requires some > conditional compilation based on compiler feature. That's probably OK > (the fallback would be to just cast without catching case 4). But we can > do better. > > The macro I have here uses a ternary with a dead branch that tries to > assign "in" to "out", I didn't know this pattern before, but is seems it's used in other places as well. Clever. > +/* > + * Check that an out-parameter that is "at least as const as" a matching > + * in-parameter. For example, skip_prefix() will return "out" that is a subset > + * of "str". So: > + * > + * const str, const out: ok > + * non-const str, const out: ok > + * non-const str, non-const out: ok > + * const str, non-const out: compile error > + * > + * See the skip_prefix macro below for an example of use. > + */ > +#define CONST_OUTPARAM(in, out) \ > + ((const char **)(0 ? ((*(out) = (in)),(out)) : (out))) I'm not sure it matters, but this is indented with spaces -- Cheers, Toon