c++ - Dependent name resolution & namespace std / Standard Library -


while answering this question (better read this "duplicate"), came following solution dependent name resolution of operator:

[temp.dep.res]/1:

in resolving dependent names, names following sources considered:

  • declarations visible @ point of definition of template.
  • declarations namespaces associated types of function arguments both instantiation context (14.6.4.1) , definition context.
#include <iostream> #include <utility>  // operator should called inside `istream_iterator` std::istream& operator>>(std::istream& s, std::pair<int,int>& p) {     s >> p.first >> p.second;     return s; }  // include definition of `istream_iterator` after declaring operator // -> temp.dep.res/1 bullet 1 applies?? #include <iterator>  #include <map> #include <fstream>  int main() {     std::ifstream in("file.in");      std::map<int, int> pp;      pp.insert( std::istream_iterator<std::pair<int, int>>{in},                std::istream_iterator<std::pair<int, int>>{} ); } 

but clang++ 3.2 , g++ 4.8 don't find operator (name resolution).

doesn't inclusion of <iterator> define "point of definition of template" istream_iterator?

edit: andy prowl points out, has nothing standard library, rather name lookup (can proven mimicking standard library multiple operator>>, @ least 1 in namespace of fake istream).


edit2: workaround, using [basic.lookup.argdep]/2 bullet 2

#include <iostream> #include <utility>  // can include <iterator> here, // definition of class template member function // instantiated when function called (or explicit instantiation) // (make sure there no relevant instantiations before definition //  of operator>> below) #include <iterator>  struct my_int {     int m;     my_int() : m() {}     my_int(int p) : m(p) {}     operator int() const { return m; } };  // operator should called inside `istream_iterator` std::istream& operator>>(std::istream& s, std::pair<my_int,my_int>& p) {     s >> p.first.m >> p.second.m;     return s; }  #include <map> #include <fstream>  int main() {     std::ifstream in("file.in");      std::map<int, int> pp;      pp.insert( std::istream_iterator<std::pair<my_int, my_int>>{in},                std::istream_iterator<std::pair<my_int, my_int>>{} ); } 

of course, can use own pair type, long workaround introduces associated class in namespace of custom operator>>.

the problem here point call operator >> being made somewhere inside std namespace, , namespace types of arguments live std.

provided compiler can find operator >> in either namespace call occurs or namespace types of arguments live (both std namespace in case), no matter whether viable or not overload resolution (which performed after name lookup), won't bother looking more overloads of operator >> in parent namespaces.

unfortunately, operator >> lives in global namespace , is, therefore, not found.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -