Prev: Why is CArchive class not derived from Cobject class in MFC
Next: What happens when Win+E is pressed?
From: Martin B. on 21 Jan 2010 10:18 Vladimir Grigoriev wrote: > Let consider an example. > > There is a class named Person. And it has a member size_t personalID and an > equality operator > > bool operator ==( const Person &lhs, size_t targetID ); > > However there is not a Person constructor with one argument equal to size_t > as a personal ID. > So there is the above equality operator and there is no a reverse equality > operator as > > bool operator ==( size_t targetID, const Personal &rhs ); // invalid > Could you explain this to me? What is the actual problem? This compiles just fine: class Foo { }; bool operator==(Foo const& lhs, size_t rhs) { return true; } bool operator==(size_t lhs, Foo const& rhs) { return true; } void f() { Foo o = Foo(); size_t i = size_t(); if( o == i) { // x } else if(i == o) { // y } else { // z } } cheers, Martin
From: Vladimir Grigoriev on 21 Jan 2010 10:22 Ulrich, the details of the Person class realization is not important. It is important that for this class there is no a great sense to introduce equality operators which compare two person or which compare size_t value or something else with the person object. However there is a great sense to compare a person with some type of identificator. Vladimir Grigoriev "Ulrich Eckhardt" <eckhardt(a)satorlaser.com> wrote in message news:pj6m27-spb.ln1(a)satorlaser.homedns.org... > Note up front: There are comp.lang.c++.moderated and comp.lang.std.c++ > (IIRC) where standard defects are better taken care of. Bitching and > moaning about a standard and that others follow or even enforce the > standard is IMHO just noise _here_. > > Vladimir Grigoriev wrote: >> There is a class named Person. And it has a member size_t personalID and >> an equality operator >> >> bool operator ==( const Person &lhs, size_t targetID ); >> >> However there is not a Person constructor with one argument equal to >> size_t as a personal ID. >> So there is the above equality operator and there is no a reverse >> equality >> operator as >> >> bool operator ==( size_t targetID, const Personal &rhs ); // invalid > > I find that already questionable, I wouldn't overload operator== for that. > Why not overload comparison with a std::string to compare the name? And > the > same again, to compare the address, etc. etc. etc. > >> Let assume further that some persons with personal IDs which belong >> to a range of personal IDs (a simple array) have gotten a bonus. And >> we are going to check which persons of some department are among them. >> >> What should we to do? Shoot ourselves? > > Invoke std::find_if in a loop: > > iterator it = begin; > while(true) { > it=find_if(begin, end, <predicate>); > if(it==end) > break; > it->give_raise(); > ++it; > } > >> Our attempt to use the algorithm std::find_first_of is prohibited by the >> Standard which requires equality comparability. So main area of classes >> and objects are overboard of STD algorithms. > > *sigh* No, you just need to learn how to use them. And yes, sometimes that > requires writing some code. > > Uli > > -- > C++ FAQ: http://parashift.com/c++-faq-lite > > Sator Laser GmbH > Geschaftsfuhrer: Thorsten Focking, Amtsgericht Hamburg HR B62 932
From: Vladimir Grigoriev on 21 Jan 2010 10:31 There is no a great sense to compare some size_t value with an object of type Person. This comparison has no sense. It is a general situation when converting some object of a class to an integral type has sense while converting an integral type to an object has no sense. For this purpose the explicit reywoard was introduced. The same is valid for operators. i.e. sometimes a op b has sense while b op a has no sense. Vladimir Grigoriev "Martin B." <0xCDCDCDCD(a)gmx.at> wrote in message news:hj9r7v$9f9$1(a)news.eternal-september.org... > Vladimir Grigoriev wrote: >> Let consider an example. >> >> There is a class named Person. And it has a member size_t personalID and >> an equality operator >> >> bool operator ==( const Person &lhs, size_t targetID ); >> >> However there is not a Person constructor with one argument equal to >> size_t as a personal ID. >> So there is the above equality operator and there is no a reverse >> equality operator as >> >> bool operator ==( size_t targetID, const Personal &rhs ); // invalid >> > > Could you explain this to me? What is the actual problem? > > This compiles just fine: > class Foo { }; > > bool operator==(Foo const& lhs, size_t rhs) { > return true; > } > > bool operator==(size_t lhs, Foo const& rhs) { > return true; > } > > void f() { > Foo o = Foo(); > size_t i = size_t(); > > if( o == i) { > // x > } > else if(i == o) { > // y > } > else { > // z > } > } > > > > cheers, > Martin
From: Vladimir Grigoriev on 21 Jan 2010 10:35 Maybe I have said something other than you are awaiting. The C++ Standard requires that for the algorithm std::find type T was EqualityComparable. Not all classes have an equality operator which satisfies this requirement. Vladimir Grigoriev "Martin B." <0xCDCDCDCD(a)gmx.at> wrote in message news:hj9r7v$9f9$1(a)news.eternal-september.org... > Vladimir Grigoriev wrote: >> Let consider an example. >> >> There is a class named Person. And it has a member size_t personalID and >> an equality operator >> >> bool operator ==( const Person &lhs, size_t targetID ); >> >> However there is not a Person constructor with one argument equal to >> size_t as a personal ID. >> So there is the above equality operator and there is no a reverse >> equality operator as >> >> bool operator ==( size_t targetID, const Personal &rhs ); // invalid >> > > Could you explain this to me? What is the actual problem? > > This compiles just fine: > class Foo { }; > > bool operator==(Foo const& lhs, size_t rhs) { > return true; > } > > bool operator==(size_t lhs, Foo const& rhs) { > return true; > } > > void f() { > Foo o = Foo(); > size_t i = size_t(); > > if( o == i) { > // x > } > else if(i == o) { > // y > } > else { > // z > } > } > > > > cheers, > Martin
From: Ulrich Eckhardt on 21 Jan 2010 11:30
Vladimir Grigoriev wrote: > Maybe I have said something other than you are awaiting. > The C++ Standard requires that for the algorithm std::find type T was > EqualityComparable. Not all classes have an equality operator which > satisfies this requirement. Generally, you can use find_if() to adapt the find algorithm to anything. The case that that searching for a T you might want to supply a U (with U!=T) that is equality-comparable to a T is nothing new. The same applies e.g. to std::map's find or std::set's find. STLport for example includes some non-standard extensions that allow doing just that, and I believe the idea is based on a defect report filed against the C++ standard. Uli -- C++ FAQ: http://parashift.com/c++-faq-lite Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 |