2

I thought that I always need the std:: qualification for std::something, unless I'm using std::something or the whole namespace std, but it seems that #include <vector> makes, for instance, std::begin availble as begin.

Why is this the case?

#include <vector>
int main(){
    int w[3];
    begin(w); // Ok, this doesn't compile, as I'm not `using std::begin` nor `using namespace std`.
    std::vector<int> v;
    v.begin(); // Ok, as this is the member function.
    begin(v); // Why does this compile? What is making this free-function available unqualifiedly?
}
Enlico
  • 23,259
  • 6
  • 48
  • 102
  • Does this answer your question? https://stackoverflow.com/questions/8111677/what-is-argument-dependent-lookup-aka-adl-or-koenig-lookup – 463035818_is_not_an_ai Jul 01 '20 at 16:42
  • [g++, clang++ and msvc](https://godbolt.org/z/zeGc76) all give the same result. – Ted Lyngmo Jul 01 '20 at 16:49
  • 3
    TL;DR of the dupe. `int w[3];` is a built in type, it does not live a namespace so no ADL. `v` on the other hand is a `std::vector` which lives in `std` and ADL finds `begin` in `std` for you. – NathanOliver Jul 01 '20 at 16:49

0 Answers0