Suggestions for convenience aliases in <type_traits>
Jonathan Wakely
jwakely@redhat.com
Wed Jun 11 10:15:00 GMT 2014
On 11/06/14 07:53 +0200, Daniel Krügler wrote:
>2014-06-10 13:17 GMT+02:00 Jonathan Wakely <jwakely@redhat.com>:
>> On 10/06/14 12:47 +0200, Daniel Krügler wrote:
>>>> Is that reasonable?
>>>
>>> Personally I consider these as bad examples for alias templates and
>>> suggest to provide corresponding class templates instead (such __and,
>>> __or, __not).
>>
>> Do you mean like this?
>>
>> template<typename _If, typename _Then, typename _Else>
>> struct __conditional_
>>
>> : conditional<_If::value, _Then, _Else>
>> { };
>>
>> With that I lose the convenience of the C++14 '_t' aliases and have to
>> write 'typename' and '::type' every time I use it.
>>
>> Or do you mean like this:
>>
>> template<typename _If, typename _Then, typename _Else>
>> struct __conditional_t_
>>
>> : conditional<_If::value, _Then, _Else>::type
>> { };
>>
>> In that case doesn't it also evaluate the prediate immediately, and so
>> has no advantage over the alias template.
>
>[Apologies for my late response, but I was completely bound by other things]
>
>The latter. There is an advantage of the second form, because you
>combine it like this
>
>SomeTemplate<__conditional_t_<X, Y, Z>>
>
>this would not necessarily instantiate the __conditional_t_ class
>template (but it would for the alias). This situation becomes
>relevant, if this
>
>__conditional_t_<X, Y, Z>
>
>is part of a potentially unevaluated part of template parameters.
OK, thanks for the explanation. In the cases I was thinking of using
this there are usually no unevaluated parameters, but I agree that if
we are going to add it to <type_traits> then your suggestion is more
flexible and has advantages for other use cases (e.g. nested uses of
std::conditional inside std::conditional).
So if we were to use that design, I think the '_t_' suffix is not
appropriate, but we could have:
template<typename _If, typename _Then, typename _Else>
struct __conditional_
: conditional<_If::value, _Then, _Else>
{ };
And then an alias for the cases where it isn't potentially unevaluated
and immediate instantiation is OK:
template<typename _If, typename _Then, typename _Else>
using __conditional_t_ = typename
__conditional_<_If::value, _Then, _Else>::type;
More information about the Libstdc++
mailing list