RFR: 8258853: Support separate function declaration and definition with ENABLE_IF-based SFINAE

Kim Barrett kbarrett at openjdk.java.net
Fri Jan 22 14:08:07 UTC 2021


On Fri, 22 Jan 2021 13:47:49 GMT, Erik Österlund <eosterlund at openjdk.org> wrote:

>> [Restarting this PR after some offline discussion with jrose.  New name for
>> the macro, and some additional commentary and tests.]
>> 
>> Please review this change which adds the ENABLE_IF_SDEFN macro.  This is
>> used in the definition of a function template when that definition is
>> separate from a declaration that uses ENABLE_IF.
>> 
>> Alternative names for the new macro are possible and even solicited.  I
>> know, I'm asking for bikeshedding.
>> 
>> The ENABLE_IF macro provides convenient syntax for function template SFINAE
>> using the new default template parameter feature of C++11.  However, this
>> macro can only be used in the function declaration (which may also be a
>> definition).
>> 
>> The syntax needed in a definition that is separate from the declaration is
>> different, but the type forms for the non-type template parameters in the
>> two places must be "equivalent" (C++14 14.5.6).  The precise form for
>> ENABLE_IF is "hidden" behind the macro.  It is not desirable to have
>> template definitions making assumptions about that form.  This suggests
>> there should be a second macro for use in the separate definition case, with
>> the two macros maintained together to ensure the necessary consistency.
>> 
>> (Note that some versions of gcc permit the unused default in some separate
>> definitions.  Other tool chains reject it.  Because of this, I was only
>> vaguely aware of (and had long forgotten) about this issue when proposing
>> ENABLE_IF, despite having extensively used similar mechanisms in other code
>> bases.)
>> 
>> This gives the following usage:
>> 
>> template<typename T, ENABLE_IF(std::is_integral<T>::value)>
>> void foo(T x) { ... }
>> 
>> and this for separate declaration and definition.  
>> 
>> template<typename T, ENABLE_IF(std::is_integral<T>::value)>
>> void foo(T x);
>> 
>> // later separate definition
>> template<typename T, ENABLE_IF_SDEFN(std::is_integral<T>::value)>
>> void foo(T x) { ... }
>> 
>> (An alternative would be to just drop the macro usage entirely, at least in
>> the separate definition case, and just accept the additional syntactic
>> complexity.  I'm not proposing that.)
>> 
>> (Since I was touching the file anyway, I also improved some of the comments
>> discussing details)
>> 
>> Testing:
>> mach5 tier1
>> This includes some new gtests for the macros.
>> There aren't any current non-test uses of ENABLE_IF_SDEFN yet.
>
> Looks good.

Thanks @fisk for review.

-------------

PR: https://git.openjdk.java.net/jdk/pull/2129


More information about the hotspot-dev mailing list