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

Kim Barrett kbarrett at openjdk.java.net
Mon Jan 18 14:13:44 UTC 2021


[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.

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

Commit messages:
 - rename to ENABLE_IF_SDEFN
 - Add ENABLE_IF_PARAM, unit tests

Changes: https://git.openjdk.java.net/jdk/pull/2129/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=2129&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8258853
  Stats: 97 lines in 2 files changed: 85 ins; 0 del; 12 mod
  Patch: https://git.openjdk.java.net/jdk/pull/2129.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/2129/head:pull/2129

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


More information about the hotspot-dev mailing list