<bdo id='LczWL'></bdo><ul id='LczWL'></ul>

      <tfoot id='LczWL'></tfoot><legend id='LczWL'><style id='LczWL'><dir id='LczWL'><q id='LczWL'></q></dir></style></legend>
      <i id='LczWL'><tr id='LczWL'><dt id='LczWL'><q id='LczWL'><span id='LczWL'><b id='LczWL'><form id='LczWL'><ins id='LczWL'></ins><ul id='LczWL'></ul><sub id='LczWL'></sub></form><legend id='LczWL'></legend><bdo id='LczWL'><pre id='LczWL'><center id='LczWL'></center></pre></bdo></b><th id='LczWL'></th></span></q></dt></tr></i><div id='LczWL'><tfoot id='LczWL'></tfoot><dl id='LczWL'><fieldset id='LczWL'></fieldset></dl></div>

      1. <small id='LczWL'></small><noframes id='LczWL'>

        依赖名称解析命名空间 std/标准库

        时间:2023-05-25

          <bdo id='VzQJD'></bdo><ul id='VzQJD'></ul>

          1. <tfoot id='VzQJD'></tfoot>

                <tbody id='VzQJD'></tbody>

              <small id='VzQJD'></small><noframes id='VzQJD'>

            • <legend id='VzQJD'><style id='VzQJD'><dir id='VzQJD'><q id='VzQJD'></q></dir></style></legend>
                <i id='VzQJD'><tr id='VzQJD'><dt id='VzQJD'><q id='VzQJD'><span id='VzQJD'><b id='VzQJD'><form id='VzQJD'><ins id='VzQJD'></ins><ul id='VzQJD'></ul><sub id='VzQJD'></sub></form><legend id='VzQJD'></legend><bdo id='VzQJD'><pre id='VzQJD'><center id='VzQJD'></center></pre></bdo></b><th id='VzQJD'></th></span></q></dt></tr></i><div id='VzQJD'><tfoot id='VzQJD'></tfoot><dl id='VzQJD'><fieldset id='VzQJD'></fieldset></dl></div>

                • 本文介绍了依赖名称解析命名空间 std/标准库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  在回答这个问题时(最好阅读这个重复"),我想出了以下解决运算符依赖名称解析的解决方案:

                  While answering this SO question (better read this "duplicate"), I came up with the following solution to dependent name resolution of an operator:

                  [temp.dep.res]/1:

                  [temp.dep.res]/1:

                  在解析依赖名称时,会考虑来自以下来源的名称:

                  In resolving dependent names, names from the following sources are considered:

                  • 在模板定义点可见的声明.
                  • 来自与实例化上下文 (14.6.4.1) 和定义上下文中的函数参数类型相关联的命名空间声明.

                  #include <iostream>
                  #include <utility>
                  
                  // this operator should be called from 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` only after declaring the 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>>{} );
                  }
                  

                  但是 clang++ 3.2 和 g++ 4.8 没有找到这个操作符(名称解析).

                  But clang++ 3.2 and g++ 4.8 don't find this operator (name resolution).

                  包含难道不是定义了模板的定义点"istream_iterator?

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

                  正如 Andy Prowl 指出的那样,这与标准库无关,而是与名称查找(可以通过模拟具有多个 operator>> 的标准库来证明,至少一个位于假 istream 的命名空间中).

                  As Andy Prowl points out, this has nothing to do with the Standard Library, but rather with name lookup (can be proven by mimicking the Standard Library with multiple operator>>, at least one in the namespace of the fake istream).

                  Edit2:一种解决方法,使用 [basic.lookup.argdep]/2 bullet 2

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

                  #include <iostream>
                  #include <utility>
                  
                  // can include <iterator> already here,
                  // as the definition of a class template member function
                  // is only instantiated when the function is called (or explicit instantiation)
                  // (make sure there are no relevant instantiations before the definition
                  //  of the operator>> below)
                  #include <iterator>
                  
                  struct my_int
                  {
                      int m;
                      my_int() : m() {}
                      my_int(int p) : m(p) {}
                      operator int() const { return m; }
                  };
                  
                  // this operator should be called from 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>>{} );
                  }
                  

                  当然,您也可以使用自己的pair类型,只要解决方法在自定义operator>>的命名空间中引入关联的类即可.

                  Of course, you can also use your own pair type, as long as the workaround introduces an associated class in the namespace of the custom operator>>.

                  推荐答案

                  这里的问题是你调用 operator >> 的点是在 内部的某个地方std 命名空间,参数类型所在的命名空间为 std.

                  The problem here is that the point where your call to operator >> is being made is somewhere inside the std namespace, and the namespace where the types of the arguments live is std.

                  只要编译器可以在调用发生的命名空间或参数类型所在的命名空间(两者都是 std 命名空间在这种情况下),无论它是否适用于重载解析(在名称查找之后执行),它都不会费心寻找 operator > 的更多重载.> 在父命名空间中.

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

                  不幸的是,您的 operator >> 位于全局命名空间中,因此找不到.

                  Unfortunately, your operator >> lives in the global namespace and is, therefore, not found.

                  这篇关于依赖名称解析命名空间 std/标准库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:gcc 可以编译可变参数模板,而 clang 不能 下一篇:我什么时候应该使用关键字“typename"?使用模

                  相关文章

                  最新文章

                  <small id='psqss'></small><noframes id='psqss'>

                • <i id='psqss'><tr id='psqss'><dt id='psqss'><q id='psqss'><span id='psqss'><b id='psqss'><form id='psqss'><ins id='psqss'></ins><ul id='psqss'></ul><sub id='psqss'></sub></form><legend id='psqss'></legend><bdo id='psqss'><pre id='psqss'><center id='psqss'></center></pre></bdo></b><th id='psqss'></th></span></q></dt></tr></i><div id='psqss'><tfoot id='psqss'></tfoot><dl id='psqss'><fieldset id='psqss'></fieldset></dl></div>

                    • <bdo id='psqss'></bdo><ul id='psqss'></ul>
                    <tfoot id='psqss'></tfoot>
                    <legend id='psqss'><style id='psqss'><dir id='psqss'><q id='psqss'></q></dir></style></legend>