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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:12:53+00:00 2026-05-25T18:12:53+00:00

Consider standard algorithms like, say, std::for_each . template<class InputIterator, class Function> Function for_each(InputIterator first,

  • 0

Consider standard algorithms like, say, std::for_each.

template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f);

As far as I can tell, there is actually no requirement placed on the relative states of the two InputIterator arguments.

Does that mean that the following is technically valid? Or is it undefined? What can I realistically expect it to do?

std::vector<int> v{0,1,2,3,4};
std::for_each(
   v.begin()+3,  // range [3,0)
   v.begin(),
   [](int){}
);

geordi tells me:

error: function requires a valid iterator range [__first, __last). [+ 13 discarded lines]

but I can’t tell how compliant this debug diagnostic is.


I came up with this question when trying to pedantically determine how explicit is defined the behaviour of the following:

std::vector<int> v; // <-- empty
std::for_each(      // <-- total no-op? stated or just left to implication?
   v.begin(),
   v.end(),
   [](int){}
);
  • 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-05-25T18:12:53+00:00Added an answer on May 25, 2026 at 6:12 pm

    The standard explicitly requires the last iterator to be reachable from the first iterator. That means that by incrementing first one should be able to eventually hit last.

    24.1 Iterator requirements

    …

    6 An iterator j is called reachable from an iterator i if and only if
    there is a finite sequence of applications of the expression ++i that
    makes i == j. If j is reachable from i, they refer to the same
    container.

    7 Most of the library’s algorithmic templates that operate
    on data structures have interfaces that use ranges. A range is a pair
    of iterators that designate the beginning and end of the computation.
    A range [i, i) is an empty range; in general, a range [i, j) refers to
    the elements in the data structure starting with the one pointed to by
    i and up to but not including the one pointed to by j. Range [i, j) is
    valid if and only if j is reachable from i. The result of the
    application of functions in the library to invalid ranges is
    undefined.

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

Sidebar

Related Questions

Consider: /* bar.h */ class bar{ /* Standard stuff omitted */ std::vector<my_obj*> foo; };
consider the following program: namespace NS2 { class base { }; template<typename T> int
Consider a standard ASP.NET web application where the user types in some numeric data
Consider the following 3 standard statements $queryString = SOME SQL SELECT QUERY; $queryResult =
Consider this problem: I have a program which should fetch (let's say) 100 records
Consider these two function definitions: void foo() { } void foo(void) { } Is
Consider the Oracle emp table. I'd like to get the employees with the top
Consider the 0/1 knapsack problem . The standard Dynamic Programming algorithm applies only when
I'd consider myself a reasonable standard CSS/XHTML chap but I'm pretty baffled by this.
Consider the following code: struct Foo { mutable int m; template<int Foo::* member> void

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.