Where should I define operator >> for my specialization of std::pair?

前端 未结 1 563
萌比男神i
萌比男神i 2020-12-16 14:25

Consider the following program:

#include 
#include 
#include 
#include 
using namespace std; //j         


        
1条回答
  •  孤街浪徒
    2020-12-16 14:48

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

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


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

    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.

    (emphasis mine)

    0 讨论(0)
提交回复
热议问题