Boost logo

Boost :

Subject: Re: [boost] Interesting variadic template trick
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2013-11-18 00:55:24


On Mon, Nov 18, 2013 at 8:42 AM, Louis Dionne <ldionne.2_at_[hidden]> wrote:
> Louis Dionne <ldionne.2 <at> gmail.com> writes:
>
>> template <typename ...T> void allow_expansion(T&&...);
>>
>> template <typename ...T>
>> struct or_ {
>> static constexpr bool value = !noexcept(
>> allow_expansion((T::value ? throw : 0)...)
>> );
>> };
>
> How embarrassing; my unit test was very poor and the above trick
> does not seem to work. However, the following was tested more rigorously
> and works (with similar performance improvements):
>
> template <typename ...T>
> constexpr bool all_pointers(T*...) { return true; }
>
> template <typename ...T>
> constexpr bool all_pointers(T...) { return false; }
>
> template <typename ...T>
> struct or_ {
> static constexpr bool value = !all_pointers(
> ((typename std::conditional<T::value, int, void*>::type)0)...
> );
> };
>
> template <>
> struct or_<> {
> static constexpr bool value = false;
> };

It requires N instantiations of std::conditional, doesn't it? In that
case is it actually faster than the naive recursive instantiation of
or_?


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk