In libc++ we currently don’t use any extensions from future standards other than for implementing the extended C++03 support. I’m proposing that we start using these extensions in libc++ to make the code base simpler, and thus easier to maintain (and in a few cases even a bit faster to compile). This would allow us to use
- variable templates
- binary literals
- fold expressions
- structured bindings
inline
variables- conditional
explicit
and probably more.
Some of these extensions don’t make much of a difference, but others can have a very large one. e.g. using conditional explicit
would cut the number of constructor overloads for tuple
and pair
almost in half.
Some of the drawbacks of using these extensions are
- less portability to new compilers
- potentially user-visible changes
- relying on compiler extensions that could be removed in the future
- this could affect people who build libc++ with a not officially supported compiler
Less portability to new compilers
This is a drawback, but I am not convinced that using these extensions is a big problem for this.
- We already rely on a lot of builtins which would have to be ported to a new compiler.
- Conforming extensions from future standards are quite likely to be implemented by many compilers. Most of these features are backported by all major compilers, which makes this a non-problem for the most part.
- most new compilers are clang-based, which means they inherit the clang extensions. For completely new compilers, it is likely that they will implement many of the extensions from other compilers to allow them to compile many projects without the need to modify the software. This is already the case with clang, which implements a lot of the GNU and MSVC extensions.
- It is also possible to only support newer language standards with new compilers.
Potentially user-visible changes
This is very unlikely given the nature of these extensions. Most of these extensions would be used for implementation details, and not for the API. The only one where that’s not the case is conditional explicit
. Given that the standard describes tuple
and pair
with an exposition-only macro EXPLICIT(condition)
, I don’t think there are many pitfall for this. Still, we would have to be a bit more cautious with that one.
Relying on compiler extensions that could be removed in the future
At least according to @AaronBallman (please point out anything that I get wrong!), this is very unlikely to happen. Basically, if we want to use anything that is not documented, we should ask the clang folks if the extension is known and make them document it. If it’s documented, it’s considered very unlikely to be removed at any point. Compilers are a lot less likely to remove extensions than standard libraries are, because having these kinds of extensions is for the most part the difference between a conditional warning (informing the user that a given construct is an extension) and a conditional error (informing the user that the feature they are trying to use is not available). This makes these extensions very low maintenance for compilers.
this could affect people who build libc++ with a not officially supported compiler
Possible, but unlikely IMO. As already said above, all major implementations already support most of the extension listed above. If you use an unsupported compiler, please comment here, and maybe consider upstreaming support for it, so we know which compilers are used with libc++ and can react to any incompatibilities.
CC @ldionne @mordante @varconst @joeloser @EricWF @crtrott @AaronBallman