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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:25:13+00:00 2026-05-23T19:25:13+00:00

What i think is that for_each is defined in standard namespace, but this code

  • 0

What i think is that for_each is defined in standard namespace, but this code actually compiles and runs with the following compiler flags. Can somebody please explain why?

  //@filename myprog.cpp 
  //g++-4.5 --std=c++0x myprog.cpp

  #include<iostream>
  #include<algorithm>

  int main()
  {
    std::vector<int> v{1,2,3,4,5};
    std::cout<<"printing the number\n";
    for_each(v.begin(),v.end(),[](int num) {//no std::for_each
        std::cout<<num<<"\t";
    });
 return 0;
 }
  • 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-23T19:25:14+00:00Added an answer on May 23, 2026 at 7:25 pm

    Converting comment to answer, the reason this works is ADL (Argument Dependent Lookup). Basically what this means is that on failing to find a suitable match for for_each in the current namespace, the compiler has a built-in rule which says, now look in other namespaces – and the set of namespaces it uses for this are the namespaces of the arguments. Once it has a set of namespaces, it will search through them to find a suitable for_each.

    The question that remains open is whether std::vector<>::iterator resides in std:: or not. Clearly in your implementation it does, which is why the appropriate for_each in std:: is found. There may be cases where this iterator is not in std:: – so to be safe (as in Alan’s comment), always get into the habit of qualifying with std::.

    Also this prevents any cases where someone else introduces another for_each (for arguments sake) in to your namespace – which may break things (in a worse case scenario – silently accepts – but breaks at run time).

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

Sidebar

Related Questions

This is for an iPhone App, but I don't think that really matters. I
The code below shows me (I think) that the for each loop is about
I've tried Key.Subtract but I think that is the numpad version. I'm looking for
I'm familiar with some mnemonic/memorization techniques for about a year. I think that this
I think this is pretty much the simplest case for mapping a Map (that
(This is not exactly the problem that I have, but it's isomorphic, and I
I'm having trouble with displaying image from db. I think that method for saving
All, I THINK that I'm looking for a function for Trilinear interpolation. Here's the
My boss believes that wizards make things simple for the user. I think they
That's the question... Do you think ASP.Net is a technology suitable for high-load sites?

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.