On Tue, Nov 10, 2020 at 7:43 PM Nick Desaulniers <ndesaulni...@google.com> wrote: > > On Mon, Nov 9, 2020 at 11:57 PM Peter Zijlstra <pet...@infradead.org> wrote: > > > > Stripping const to delcare another variable is useful though. Sure C has > > sharp edges, esp. if you cast stuff, but since when did that stop anyone > > ;-) > > > > The point is, C++ has these very nice template helpers that can strip > > qualifiers, I want that too, for much of the same reasons. We might not > > have templates :-(, but we've become very creative with our > > pre-processor. > > > > Surely our __unqual_scalar_typeof() cries for a better solution. > > Yeah, and those macros bloat the hell out of our compile times, for > both compilers. I think it's reasonable to provide variants of > typeof() that strip qualifiers.
GCC 14+ and clang 19+ provide __typeof_unqual__() [1] operator in all c modes. Linux kernel 6.15 provides the USE_TYPEOF_UNQUAL macro that can be used to check availability of __typeof_unqual__(). [1] https://en.cppreference.com/w/c/keyword/typeof_unqual Uros. > Some questions to flesh out more of a design. > > Would we want such a feature to strip all qualifiers or just specific > individual ones? The more specific variants could be composed, ie. > > nonconst_typeof(x) y = x + 1; > nonvol_typeof(z) w = z + 1; > #define nonqual_typeof(v) nonconst_typeof(nonvol_typeof(v)) > nonqual_typeof(v) k = v + 1; > vs just: > nonqual_typeof(v) k = v + 1; > > When I think of qualifiers, I think of const and volatile. I'm not > sure why the first post I'm cc'ed on talks about "segment" qualifiers. > Maybe it's in reference to a variable attribute that the kernel > defines? Looking at Clang's Qualifier class, I see const, volatile, > restrict (ah, right), some Objective-C stuff, and address space > (TR18037 is referenced, I haven't looked up what that is) though maybe > "segment" pseudo qualifiers the kernel defines expand to address space > variable attributes? > > Maybe stripping all qualifiers is fine since you can add them back in > if necessary? > > const volatile foo; > const nonqual_typeof(foo) bar = foo; // strips off both qualifiers, > re-adds const to bar > -- > Thanks, > ~Nick Desaulniers