Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to write a SFINAE test of "for-eachability" of a type?

I have used SFINAE expressions to test for if a type supports operator<<

namespace details
{
  template<typename T>
  struct sfinae_true : std::true_type
  {
  };

  template<typename T>
  sfinae_true<decltype (std::declval<std::ostream &> () << std::declval<T const &> ())> test_for_ostream (int);

  template<typename T>
  std::false_type test_for_ostream (long);
}

template<typename T>
struct supports_ostream : decltype (details::test_for_ostream<T> (0))
{
};

What I would like to test is if this a type T can be iterated over like this

for (auto && v : vs) {} // vs is T const &

The dilemma is that this is a statement and not an expression which makes it incompatible to use with decltype

I was thinking to use lambdas to convert a statement to an expression like this

auto x = [] () { for (auto && v : vs) {}; return 0; } (); // vs is T const &

However decltype of expressions containing lambdas seems to be explicitly forbidden:

// Won't compile in clang, gcc nor VC++
using x_t = decltype ([] () { for (auto && v : vs) {}; return 0; } ()); // vs is T const &

So that disqualifies it for use in a test function like this:

namespace details
{
  template<typename T>
  sfinae_true<decltype (
    [] () { for (auto && v : std::declval<T const &> ()) ; } () 
    )> test_for_container (int); 
  // Won't work because lambdas aren't allowed in unevaluated contexts

  template<typename T>
  std::false_type test_for_container (long);
}

template<typename T>
struct is_container : decltype (details::test_for_container<T> (0))
{
};

So I have run out of ideas, so I thought perhaps someone @Stackoverflow can come up with something interesting.

PS.

I can somewhat understand why decltype ([] () {}) isn't allowed but decltype ([] () {} ()) should always be well-defined ie void.

like image 712
Just another metaprogrammer Avatar asked Oct 27 '15 21:10

Just another metaprogrammer


1 Answers

For the majority of cases the following trait should suffice:

#include <type_traits>
#include <utility>
#include <iterator>

namespace detail
{
    using std::begin;
    using std::end;

    template <typename T>
    auto is_range_based_iterable(...)
        -> std::false_type;

    template <typename T
            , typename I = typename std::decay<decltype(std::declval<T>().begin())>::type>
    auto is_range_based_iterable(int)
        -> decltype(std::declval<T>().begin()
                  , std::declval<T>().end()
                  , ++std::declval<I&>()
                  , void()
                  , std::integral_constant<bool,
                       std::is_convertible<decltype(std::declval<I&>() != std::declval<I&>()), bool>::value
                    && !std::is_void<decltype(*std::declval<I&>())>::value
                    && std::is_copy_constructible<I>::value
                  >{});

    template <typename T
            , typename I = typename std::decay<decltype(begin(std::declval<T>()))>::type>
    auto is_range_based_iterable(char)
        -> decltype(begin(std::declval<T>())
                  , end(std::declval<T>())
                  , ++std::declval<I&>()
                  , void()
                  , std::integral_constant<bool,
                       std::is_convertible<decltype(std::declval<I&>() != std::declval<I&>()), bool>::value
                    && !std::is_void<decltype(*std::declval<I&>())>::value
                    && std::is_copy_constructible<I>::value
                  >{});
}

template <typename T>
struct is_range_based_iterable : decltype(detail::is_range_based_iterable<T>(0)) {};

Test:

#include <vector>
#include <array>

int main()
{
    static_assert(is_range_based_iterable<std::vector<int>>::value, "!");
    static_assert(is_range_based_iterable<std::array<int, 5>>::value, "!");
    static_assert(is_range_based_iterable<int(&)[5]>::value, "!");
}

DEMO

like image 88
Piotr Skotnicki Avatar answered Oct 24 '22 00:10

Piotr Skotnicki