0

I'm surprised that this simple program compiles:

#include <vector>
#include <stdio.h>

int main()
{
    std::vector<int> v = {1,2,3};
    auto x = v.end();
    if (x == end(v)) // << == end used without `std::`
        printf("v.end == end(v)\n");
}

On one of the lines, std::end is used without specifying std:: namespace. Surprisingly, this compiles even though there is no using namespace std. What's going on, why does it compile without any errors/warnings?

https://godbolt.org/z/7e9xMh361 (compiles with gcc/clang/msvc)

Pavel P
  • 15,789
  • 11
  • 79
  • 128
  • Whoever voted to close it: the [ADL](https://stackoverflow.com/questions/8111677/what-is-argument-dependent-lookup-aka-adl-or-koenig-lookup) question is the answer to *this* question. Is in no way it's a duplicate. – Pavel P Mar 01 '23 at 22:53
  • 3
    Reading the [first answer](https://stackoverflow.com/a/8111750/1294207) to the question highlights the reason for it compiling without errors and warnings. The answer to your question would be "Because of ADL, here is a link to an excellent explanation". Isn't this the same effect as closing as a duplicate? – Fantastic Mr Fox Mar 01 '23 at 23:17
  • 2
    Your question is still good, it just serves as an excellent signpost and example for the ADL Answer. – Fantastic Mr Fox Mar 01 '23 at 23:17
  • 4
    Duplicate closure is about answers, not word for word copies of questions. Did you not get the answer you wanted? Why should it be repeated again and again under this post too? – StoryTeller - Unslander Monica Mar 02 '23 at 07:28
  • 1
    @StoryTeller-UnslanderMonica Different questions with the same answer are *not* duplicates. – Kevin Krumwiede Mar 02 '23 at 19:10
  • 2
    @KevinKrumwiede Different questions with the same answer *are* duplicates. – n. m. could be an AI Mar 02 '23 at 19:40
  • 1
    Does the answer under the ADL question answer your question too? If not, what kind of answer do you expect? – n. m. could be an AI Mar 02 '23 at 19:46
  • 2
    @KevinKrumwiede - Under the SO model, following much deliberation and voting on meta, they **are**. The point is to collect and curate quality information, not fragment it across a bezillion "different" questions that are always answered the same. – StoryTeller - Unslander Monica Mar 02 '23 at 19:49
  • @StoryTeller: Do you happen to have a link to the meta discussion to which you are referring? Was it on Stack Overflow Meta or StackExchange Meta? – Andreas Wenzel Mar 03 '23 at 02:09

0 Answers0