RFR: 8251274: Provide utility types for function SFINAE using extra template parameters
Kim Barrett
kim.barrett at oracle.com
Wed Sep 2 01:43:50 UTC 2020
> On Sep 1, 2020, at 1:26 PM, Lois Foltan <lois.foltan at oracle.com> wrote:
>
> On 9/1/2020 4:55 AM, Kim Barrett wrote:
>>> On Aug 31, 2020, at 9:43 AM, Kim Barrett <kim.barrett at oracle.com> wrote:
>>>
>>>> On Aug 31, 2020, at 5:38 AM, Erik Österlund <erik.osterlund at oracle.com> wrote:
>>>>
>>>> Hi Kim,
>>>>
>>>> Looks good.
>>> Thanks
>>>
>>>> Thanks,
>>>> /Erik
>>>>
>>>> On 2020-08-31 11:18, Kim Barrett wrote:
>>>>> […] OK. Then here’s a new webrev. (The gtest file has no changes now, but still shows up in the
>>>>> webrev because of the change then revert in the patch series.) Just a full, since there seems
>>>>> little point in an incremental.
>>>>>
>>>>> https://cr.openjdk.java.net/~kbarrett/8251274/open.01/
>> Given the changes we’ve discussed, the summary for the bug should probably be changed from
>> “utility types” to “utility macro”.
>>
>
> Hi Kim,
>
> This looks good.
Thanks.
> After reading through the discussion with Erik, I am glad about the resulting ENABLE_IF macro. I do have one question/comment which might just be for my clarification. I am assuming I can also still use this new C++11 extra anonymous non-type template parameter with a default value as the return type?
I'm not sure I understand your question, so apologies if I've misinterpreted
and answered something else.
The new macro, and the new in C++11 mechanism it uses, is only in template
parameter lists.
You can still use the old technique of a SFINAE expression for a function's
result type. That is, this still works:
template<typename T>
typename EnableIf<std::is_integral<T>::value, bool>::type
foo(T x) { ... }
is a function returning bool that is only applicable if the argument is an
int. Though with C++11 one could instead write that as
template<typename T>
std::enable_if_t<std::is_integral<T>::value, bool>
foo(T x) { ... }
But why would you, when you can separate the SFINAE part from the signature,
improving readability:
template<typename T, ENABLE_IF(std::is_integral<T>::value)>
bool foo(T x) { … }
> In addition, if I am allowed to use this as the return type, I further assume that it is also allowed to use a single template parameter that is a conjunction of expressions for the return type too?
In each of the above examples, `std::is_integral<T>::value` is in a
syntactic position that must be an expression. Any boolean expression will
do, including a conjunction of subexpressions.
More information about the hotspot-dev
mailing list