[type_traits] Request: is_iterable

I'd like to make a request for an is_iterable<T> type trait, which detects if a type can be used in C++11 range-based for. I have a prototype which currently meets my needs (I can make it available; it detects if both std::begin(c) and std::end(c) can be called), but I'm wondering: a) What should the name be? (I'm not married to is_iterable) b) Should there be a second template parameter indicating the type returned by derefencing the iterator, defaulting to don't care. c) What should the C++03 behavior be? d) Is there someone with more expertise than I willing to implement a robust version of it? Does this sound like a good idea? -- Nevin ":-)" Liber <mailto:nevin@eviloverlord.com> (847) 691-1404

I'd like to make a request for an is_iterable<T> type trait, which detects if a type can be used in C++11 range-based for. I have a prototype which currently meets my needs (I can make it available; it detects if both std::begin(c) and std::end(c) can be called), but I'm wondering:
a) What should the name be? (I'm not married to is_iterable)
is_range ?
Does this sound like a good idea?
I think it would be useful. Regards, Nate

2013/2/2 Nathan Ridge <zeratul976@hotmail.com>
Nevin Liber <nevin@eviloverlord.com>:
I'd like to make a request for an is_iterable<T> type trait, which
detects
if a type can be used in C++11 range-based for. I have a prototype which currently meets my needs (I can make it available; it detects if both std::begin(c) and std::end(c) can be called), but I'm wondering:
a) What should the name be? (I'm not married to is_iterable)
is_range ?
+1
Does this sound like a good idea?
+1 Regards Kris

Hi Nevin, Krzysztof Czainski wrote:
Nathan Ridge:
Nevin Liber:
I'd like to make a request for an is_iterable<T> type trait, which
detects
if a type can be used in C++11 range-based for. I have a prototype which currently meets my needs (I can make it available; it detects if both std::begin(c) and std::end(c) can be called), but I'm wondering:
a) What should the name be? (I'm not married to is_iterable)
is_range ?
+1
+1
Does this sound like a good idea?
+1
There have been a few attempts in the past:
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-quo... 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. Thanks, Nate

On 2 February 2013 10:27, Nathan Crookston <nathan.crookston@gmail.com>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-quo...
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@eviloverlord.com> (847) 691-1404
participants (4)
-
Krzysztof Czainski
-
Nathan Crookston
-
Nathan Ridge
-
Nevin Liber