In any case, I concur with Louis that this seems like it’d have to be done at the C++ level, not the preprocessor level.
The semantics of my hypothetical “__using_if_exists QUALNAME;” declaration are that it does a name lookup on QUALNAME, and then if the lookup fails it does nothing, and if the lookup succeeds it acts like a regular
usingdeclaration. This feels like it should be super easy to implement as a compiler extension, right?
Yes, that’s exactly what I was asking. I wondered whether folks could spot a fundamental issue with doing this, but it looks like perhaps there is none.
Bikeshed: __using_if_exists, __using_or_ignoring, __using_if_present, __using_unless_absent, __using_maybe_absent.
But, doesn’t libc++ also need to work with GCC and maybe other compilers too? So won’t you need to convince those compilers to implement whatever you pick here, or else keep the existing preprocessor hacks around forever anyway?
AFAICT, libc++ works with recent-ish GCCs and Clang, that’s it. There’s been some work for it to work on other compilers, but none of it is maintained, and all of it is probably broken – as far as I’m aware.
If we went down that route, I’d kindly ask GCC to implement the same builtin, and if they say no, I could always resort to:
define _LIBCPP_USING_IF_EXISTS(…) using VA_ARGS
define _LIBCPP_USING_IF_EXISTS(…) __using_if_exists VA_ARGS
Or whatever form that ends up taking. I might have to keep existing workarounds for a little bit, but it would be reasonable to require that this extension be supported in order to add support for a new platform. Otherwise the headers just become a huge spaghetti.
It’s always best, when encountering issues like this, to solve them in the most generic way possible so that laypeople could benefit from our efforts as well. That’s why I would favor solving this using reflection/reification techniques that we will want users to eventually be using anyway. My original detailed reply from early this morning seems not to have gone through, looking at the thread online, so I’ll reproduce it here just to be sure it’s considered, since this is a perfect motivating case for these more general techniques. Thanks and good luck,
My original reply: