Boost logo

Boost :

Subject: Re: [boost] Interesting variadic template trick
From: Louis Dionne (ldionne.2_at_[hidden])
Date: 2013-11-17 23:42:46


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;
    };

Regards,

Louis Dionne


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