Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:06:47+00:00 2026-06-09T15:06:47+00:00

I’ve made is_iterable template as below – which checks that free functions begin/end are

  • 0

I’ve made is_iterable template as below – which checks that free functions begin/end are available (in the context of ADL) and return proper iterator object. (Which is the requirement for iterable object in ranged-for loop)

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

template <typename T>
typename std::add_rvalue_reference<T>::type declval(); // vs2010 does not support std::declval - workaround

template <bool b>
struct error_if_false;

template <>
struct error_if_false<true>
{
};

template<typename U>
struct is_input_iterator
{
    enum {
        value = std::is_base_of<
            std::input_iterator_tag,
            typename std::iterator_traits<U>::iterator_category
        >::value
    };
};

template<typename T>
struct is_iterable
{
    typedef char yes;
    typedef char (&no)[2];

    template<typename U>
    static auto check(U*) -> decltype(
        error_if_false<
            is_input_iterator<decltype(begin(declval<U>()))>::value
        >(),
        error_if_false<
            is_input_iterator<decltype(end(declval<U>()))>::value
        >(),
        error_if_false<
            std::is_same<
                decltype(begin(declval<U>())),
                decltype(end(declval<U>()))
            >::value
        >(),
        yes()
    );

    template<typename>
    static no check(...);

public:
    static const bool value = (sizeof(check<typename std::decay<T>::type>(nullptr)) == sizeof(yes));
};

#include <cstdio>
void write(int a)
{
    printf("%d\n", a);
}

template<typename T>
typename std::enable_if<is_iterable<T>::value>::type write(const T& a)
{
    for (auto i = begin(a), e = end(a); i != e; ++i) {
        write(*i);
    }
}

int main()
{
    write(10);

    std::vector<int> a;
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);
    a.push_back(4);

    write(a);
}

Above code exactly works as intended in vs2010, but not in gcc. Moreover, when I put the some random free function named ‘begin’, as below, it becomes broken even in vs2010.

int begin(int)
{
}

How can I make the code works? Also, any suggestion that improves the “is_iterable” concept checker would be appreciated.

added: “write” function is just a concept demonstration example – please don’t invest your precious time to it. (I know it’s not the best code 🙁 ) The part needs some attention is ADL behaviour and “is_iterable” template 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:06:48+00:00Added an answer on June 9, 2026 at 3:06 pm

    After adding std:: to begin/end – your code compiles and runs under gcc-4.6

    Improvement suggestion: replace error_if_false with of enable_if

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.