Boost logo

Boost :

Subject: Re: [boost] [type_traits] Request: is_iterable
From: Nevin Liber (nevin_at_[hidden])
Date: 2013-02-04 19:02:50


On 2 February 2013 10:27, Nathan Crookston <nathan.crookston_at_[hidden]>wrote:

> http://boost.2283326.n4.nabble.com/is-range-metafunction-td2650470.html
>
> http://boost.2283326.n4.nabble.com/range-Trying-to-make-an-quot-is-range-quot-metafunction-td2580209.html
>
> It seems most have concluded it's not possible in the general case. I
> think you could get close enough to count, however. I'd be interested to
> see the code, myself.
>

Here it is (C++11):

    namespace detail
    {
        template<typename T>
        class is_iterable
        {
            template<typename U,
                     typename = decltype(std::begin(std::declval<U>()))>
            static std::true_type has_begin(int);

            template<typename>
            static std::false_type has_begin(...);

            template<typename U,
                     typename = decltype(std::end(std::declval<U>()))>
            static std::true_type has_end(int);

            template<typename>
            static std::false_type has_end(...);

            typedef decltype(has_begin<T>(0)) has_begin_type;
            typedef decltype(has_end<T>(0)) has_end_type;

        public:
            typedef std::integral_constant<bool, has_begin_type() &&
has_end_type()> type;
        };
    } // detail namespace

    template<typename T>
    struct is_iterable : detail::is_iterable<T>::type {};

I don't know how well it handles namespace issues, though.

-- 
 Nevin ":-)" Liber  <mailto:nevin_at_[hidden]>  (847) 691-1404

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