RFR: 8258853: Support separate function declaration and definition with ENABLE_IF-based SFINAE [v2]
Kim Barrett
kbarrett at openjdk.java.net
Fri Jan 22 14:48:12 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.
Kim Barrett has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains three additional commits since the last revision:
- Merge branch 'master' into enable_if_param
- rename to ENABLE_IF_SDEFN
- Add ENABLE_IF_PARAM, unit tests
-------------
Changes:
- all: https://git.openjdk.java.net/jdk/pull/2129/files
- new: https://git.openjdk.java.net/jdk/pull/2129/files/bbd78dff..5764b7e2
Webrevs:
- full: https://webrevs.openjdk.java.net/?repo=jdk&pr=2129&range=01
- incr: https://webrevs.openjdk.java.net/?repo=jdk&pr=2129&range=00-01
Stats: 79171 lines in 2225 files changed: 28825 ins; 32603 del; 17743 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