c++ - Why isn't std::find() using my operator==? -
In the following snippet, I have overloaded the comparison of my pair of
operator == string
Type in with but for some reason, the compiler is not searching for my operator as a match for the search function. why not?
Edit: Thanks for all the suggestions for the options, but I still want to understand why . This code looks like it should work; I want to know why this does not happen. #include & lt; Vector & gt; # Include & lt; Utility & gt; #include & lt; String & gt; # Include & lt; Algorithm & gt; Typedef std :: pair & lt; Std :: string, int & gt; RegPair; Typedef std :: vector & lt; RegPair & gt; RegPairSeq; Bull Operator == (Const Repechier & LHS, Constant Strode :: String & amp; rhs) {Return lhs.first == rhs; } Int main () {RegPairSeq sequence; Std :: string foo ("foo"); // stuff that is not important std :: find (sequence.begin (), sequence.end (), foo); // g ++: Error: 'operator ==' has no match for '__first' in __gnu_cxx :: __ normal_iterator & LT; _Iterator, _Container & gt; :: operator * [_Iterator = std :: pair & lt; Std :: basic_string & lt; Four, std :: char_traits & lt; Four>, std :: defluent & lt; Four & gt; & Gt;, integer & gt; *, _Container = std :: vector & lt; Std :: pair & lt; Std :: basic_string & lt; Four, std :: char_traits & lt; Four>, std :: defluent & lt; Four & gt; & Gt; Integer & gt ;, std :: communicator & lt; Std :: pair & lt; Std :: basic_string & lt; Four, std :: char_traits & lt; Four>, std :: defluent & lt; Four & gt; & Gt ;, int & gt; & Gt; & Gt;] () == __val '// Playing ++: Error: invalid operands to binary expression (' std :: pair & lt; std :: basic_string & lt; four & gt ;, integer & gt; ' And 'std :: basic_string & lt; four & gt; constants')}
The problem is that std :: find is a function template and it seems right to use logic dependent view (ADL) to use operator == . Both the code of the argument in the code> name space ( std :: pair & lt; std :: string, int & gt; and Std :: string ), hence ADL std name space in it some operator == (that does not matter, no matter what, in the standard library a lot Are all and if you have included & lt; string & gt; at least that can compare two std :: basic_string & lt; T & gt; items is). Because a operator == is found in overload std namespace, ADL prevents the detection of scops being closed. Your surcharge, which is located in the global name space, has never been found. Name lookup before the overload resolution; It does not make any difference in the name of the logic.
Comments
Post a Comment