• <small id='i0snC'></small><noframes id='i0snC'>

    • <bdo id='i0snC'></bdo><ul id='i0snC'></ul>

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

        我应该在哪里定义操作符 &gt;&gt;对于我对 std::pair 的专业化?

        时间:2024-08-14
        <tfoot id='Y9mKs'></tfoot>

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

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

                  本文介绍了我应该在哪里定义操作符 &gt;&gt;对于我对 std::pair 的专业化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑以下程序:

                  #include <iostream>
                  #include <iterator>
                  #include <vector>
                  #include <utility>
                  using namespace std; //just for convenience, illustration only
                  
                  typedef pair<int, int> point; //this is my specialization of pair. I call it point
                  
                  istream& operator >> (istream & in, point & p)
                  {
                      return in >> p.first >> p.second;
                  }
                  
                  int main()
                  {
                      vector<point> v((istream_iterator<point>(cin)), istream_iterator<point>()); 
                      //             ^^^                         ^^^        
                      //extra parentheses lest this should be mistaken for a function declaration
                  }
                  

                  这无法编译,因为一旦 ADL 在命名空间 std 中找到运算符 >>,它就不再考虑全局范围,无论在 std 中找到的运算符是否是可行的候选者.这比较不方便.如果我将我的运算符 >> 的声明放入命名空间 std(这在技术上是非法的)中,代码将按预期编译.除了将 point 设为我自己的类,而不是将其定义为 std 命名空间中模板的特化,还有什么办法可以解决这个问题?

                  This fails to compile because as soon as ADL finds operator >> in namespace std it doesn't consider the global scope any more regardless of whether the operator found in std was a viable candidate or not. This is rather inconvenient. If I place the declaration of my operator >> into namespace std (which is technically illegal) the code compiles well as expected. Is there any way to resolve this issue other than make point my own class rather than typedefing it as a specialization of a template in std namespace?

                  提前致谢

                  推荐答案

                  namespace std 中添加 operator>> 的重载是被禁止的,但是添加了模板特化有时是允许的.

                  Adding an overload of operator>> in namespace std is forbidden, but adding a template specialization is sometimes allowed.

                  然而,这里没有用户定义的类型,标准类型上的操作符不是你重新定义的.专门化 operator>>(istream&, pair) 是合理的.

                  However, there are no user-defined types here, and the operators on standard types are not yours to redefine. Specializing operator>>(istream&, pair<mytype, int>) would be reasonable.

                  section [namespace.std](n3290 的第 17.6.4.2.1 节)说

                  section [namespace.std] (section 17.6.4.2.1 of n3290) says

                  如果 C++ 程序向命名空间 std 或命名空间 std 中的命名空间添加声明或定义,则它的行为是未定义的,除非另有说明.程序可以将任何标准库模板的模板特化添加到命名空间std,前提是声明依赖于用户定义的类型并且特化满足标准库的要求原始模板,并未明确禁止.

                  The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.

                  (强调我的)

                  这篇关于我应该在哪里定义操作符 &gt;&gt;对于我对 std::pair 的专业化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:插入 STL 映射是否会使其他现有迭代器无效? 下一篇:STL 迭代器上下文中的奇异值和非奇异值是什么?

                  相关文章

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

                  <legend id='XSqM4'><style id='XSqM4'><dir id='XSqM4'><q id='XSqM4'></q></dir></style></legend>
                • <tfoot id='XSqM4'></tfoot>

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

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