0

When writing code like this

std::vector<int> test;
std::find(test.begin(), test.end(), 5);

My IDE (Microsoft Visual Studio 2017 with the Resharper plugin ) tells me that the namespace qualifier on the second line is redundant and the code can be rewritten like this:

std::vector<int> test;
find(test.begin(), test.end(), 5);

It compiles just fine. Note that at no point do I use using namespace std or using std::find. This does not apply only to the std namespace, I've seen it on other occasions.

Under which exact circumstances is omitting the namespace qualifier allowed, provided no using was used? Where in the language standard is this defined and what's the rationale for it?

After some testing my assumption is, that if one parameter of the function lies in the same namespace as the function, the namespace of the function is automatically inferred, but I would like to see some official reference.

Cœur
  • 37,241
  • 25
  • 195
  • 267
LukeG
  • 638
  • 10
  • 20
  • 1
    Note that this is not guaranteed to work because there is no guarantee that `std::vector::iterator` is originally defined in `namespace std`. It could be defined in some (possibly nested) internal namespace. – Brian Bi Jul 27 '17 at 20:54
  • @Brian Thanks for noting that. That seems like a "feature" that can create weird platform dependencies (like the one you mentioned) and create subtle bugs that are hard to trace... – LukeG Jul 27 '17 at 20:57
  • 1
    This inspection has the lowest priority, and just shows you that the qualifier can be omitted in this case, not that it necessarily should be. ReSharper team will also make changes to this inspection when ADL lookup is present to make it less confusing, please follow https://youtrack.jetbrains.com/issue/RSCPP-19825. – Igor Akhmetov Aug 03 '17 at 08:38

0 Answers0