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

  • SEARCH
  • Home
  • 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 9180837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:08:53+00:00 2026-06-17T18:08:53+00:00

I sort of assumed that range based for loops would support C-style strings void

  • 0

I sort of assumed that range based for loops would support C-style strings

void print_C_str(const char* str)
{
    for(char c : str)
    {
        cout << c;
    }
}

However this is not the case, the standard [stmt.ranged] (6.5.4) says that range-based-for works in one of 3 possibilities:

  1. The range is an array
  2. The range is a class with a callable begin and end method
  3. There is ADL reachable in an associated namespace (plus the std namespace)

When I add begin and end functions for const char* in the global namespace I still get errors (from both VS12 and GCC 4.7).

Is there a way to get range-based-for loops to work with C style strings?

I tried adding an overload to namespace std and this worked but to my understanding it’s illegal to add overloads to namespace std (is this correct?)

  • 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-17T18:08:54+00:00Added an answer on June 17, 2026 at 6:08 pm

    If you write a trivial iterator for null-terminated strings, you can do this by calling a function on the pointer that returns a special range, instead of treating the pointer itself as the range.

    template <typename Char>
    struct null_terminated_range_iterator {
    public:
        // make an end iterator
        null_terminated_range_iterator() : ptr(nullptr) {}
        // make a non-end iterator (well, unless you pass nullptr ;)
        null_terminated_range_iterator(Char* ptr) : ptr(ptr) {}
    
        // blah blah trivial iterator stuff that delegates to the ptr
    
        bool operator==(null_terminated_range_iterator const& that) const {
            // iterators are equal if they point to the same location
            return ptr == that.ptr
                // or if they are both end iterators
                || is_end() && that.is_end();
        }
    
    private:
        bool is_end() {
            // end iterators can be created by the default ctor
            return !ptr
                // or by advancing until a null character
                || !*ptr;
        }
    
        Char* ptr;
    }
    
    template <typename Char>
    using null_terminated_range = boost::iterator_range<null_terminated_range_iterator<Char>>;
    // ... or any other class that aggregates two iterators
    // to provide them as begin() and end()
    
    // turn a pointer into a null-terminated range
    template <typename Char>
    null_terminated_range<Char> null_terminated_string(Char* str) {
        return null_terminated_range<Char>(str, {});
    }
    

    And usage looks like this:

    for(char c : null_terminated_string(str))
    {
        cout << c;
    }
    

    I don’t think this loses any expressiveness. Actually, I think this one is clearer.

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

Sidebar

Related Questions

Sort of a methods/best practices question here that I am sure has been addressed,
Is it possible to sort a list based off one item? For instance, if
Suppose that one has some lock based code like the following where mutexes are
I'm interested in writing a PHP script (I do welcome language-agnostic suggestions) that would
Sort of noob-rails question ;): I've got 2 actions in my controller - index
...@Sort bit) AS SELECT .............. and I want to Order only if Sort =
A (sort of - it's not directly to do with C#) related question in
what sort of conditions can we use for branching in jinja2? I mean can
Comparison sort is opted for in most of the scenarios where data needs to
I sort of understand this, at least the function of generators (I've used them

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.