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 9207061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:18:29+00:00 2026-06-18T00:18:29+00:00

For logging code, I would like to detect whether given argument to a template

  • 0

For logging code, I would like to detect whether given argument to a template function can be iterated over using the tools from Boost.Range or not. Obviously I need to instantiate different code whether it is or not, so I need SFINAE, possibly (well, certainly) combined with boost::enable_if. I’ve tried detecting whether begin and end free functions are defined, like this:

namespace is_range_impl {
    template <typename T> T &make();
    struct any { template <class T> any(T const&); };
    struct not_range {};
    not_range begin(const any &);
    not_range end(const any &);
    struct no_type { char x[8]; };
    typedef char yes_type;
    template <typename T> yes_type check(const T &t);
    no_type check(const not_range &t);
    using boost::begin;
    using boost::end;
    template <typename T> struct is_range_impl {
        enum e {
            value = (sizeof(check(begin(make<T>()))) == sizeof(yes_type) &&
                     sizeof(check(end(make<T>()))) == sizeof(yes_type)),
        };
    };
}

template <typename T>
struct is_range : public is_range_impl::is_range_impl<T> {};

template <typename T>
typename boost::disable_if<is_range<T> >::type repr(std::ostream &s, const T &v)
{ ... }

template <typename T>
typename boost::enable_if<is_range<T> >::type repr(std::ostream &s, const T &v)
{ ... }

But instead of silently failing when boost::begin and boost::end are not well-defined, it fails loudly with error that

'type' : is not a member of 'boost::mpl::eval_if_c<C,F1,F2>'
    [C=false, F1=boost::range_const_iterator<void *>, F2=boost::range_mutable_iterator<void *>]

in the following code in boost/range/iterator.hpp:63:

typedef BOOST_RANGE_DEDUCED_TYPENAME
        mpl::eval_if_c< is_const<C>::value,
                        range_const_iterator< typename remove_const<C>::type >,
                        range_mutable_iterator<C> >::type type;

(I have boost 1.51, but 1.52 lists no changes and 1.53 alpha lists two bug-fixes, but neither seems to be related)

So is there some better way to detect ranges? While I am stuck with some C++03 compilers and probably will be for quite a long while longer, I would like to make it as easily switchable to C++11 (where free begin and end available via ADL appear to be all that’s needed).

Specifically the compilers are Visual C++ 9.0 and GCC 4.5. SFINAE support is adequate in both.

  • 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-18T00:18:30+00:00Added an answer on June 18, 2026 at 12:18 am

    I believe the metafunction you need to use is has_range_iterator.

    Running on LWS

    #include <iostream>
    #include <string>
    #include <utility>
    
    #include <boost/range.hpp>
    
    template <typename T>
    typename boost::disable_if<boost::has_range_iterator<T> >::type repr(const T &, const std::string& name)
    { std::cout << name << " is not a range" << std::endl; }
    
    template <typename T>
    typename boost::enable_if<boost::has_range_iterator<T> >::type repr(const T &, const std::string& name)
    { std::cout << name << " is a range" << std::endl; }
    
    struct foo{};
    struct bar
    {
       typedef int iterator;
        typedef const int const_iterator;
        int begin(){ return 0;};
        int end(){ return 1;};
    };
    
    
    int main()
    {
        int i;
        repr(i, "int");
        int array[10];
        repr(array, "int array");
        std::string str;
        repr(str, "std::string");
        foo f;
        repr(f, "foo");
        bar b;
        repr(b, "bar");
    
        std::pair<int, int> p;
        repr(p, "pair"); // it does make a mistake here, because std::pair<int, int> looks like range, but int is not valid iterator (cannot be dereferenced)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For debugging / performance tests I would like to dynamically add logging code to
I'm writing python package/module and would like the logging messages mention what module/class/function they
I would like to put some logging statements within test function to examine some
I'm writing some code that handles logging xml data and I would like to
I want to wrap every function call with some logging code. Something that would
My log4net is logging to the visual studio output windows but what code can
this week I'm having problems logging in LinkedIn using ruby mechanize. My code is
I am creating a new event source and logging a message using the code
We are deploying web parts to SharePoint 2007 and would like to include logging
I would like to instrument JavaScript code in order to log values of global

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.