Re: operator > Against Addresses Of Member Functions
On Sep 1, 5:19 am, Jiang <goo.mai...@yahoo.com> wrote:
On Sep 1, 4:14 pm, Le Chaud Lapin <jaibudu...@gmail.com> wrote:
What's the preferred method for ascertaining truth of
p1 > p2
No, there is no way to do the comparison correctly, since the
behavior for above comparison is unspecified by the current
standard(In 5.9/p2). Also I do not see the point for this kind
of comparison.
I guess I should provide some context for discussion.
In the sentence:
"Water is wet."
"Water" is the subject, and "is wet" is the predicate. The predicate
consists of the verb "is" and object "wet". I need to maintain the "is
wet" part as an object that can be assigned, serialized, stored in
set<>, map<>, etc. I construct this predicate in a
Relational_Predicate<>:
template <typename Element> struct Relational_Predicate
{
typedef bool (Element::*Relation) (const Element &object) const;
Relation relation;
Element object;
Relational_Predicate (Relation relation, const Element &object) :
relation(relation), object(object) {}
bool complements (const Element &subject) const
{
return (subject.*relation)(object);
}
// ...lot of other stuff
} ;
I have several classes that naturally lend themselves to this concept:
struct Space
{
bool operator == (const Space &) const;
bool operator != (const Space &) const;
bool operator > (const Space &) const;
bool operator < (const Space &) const;
bool operator >= (const Space &) const;
bool operator <= (const Space &) const;
bool is_subspace_of (const Space &) const;
bool is_proper_subspace_of (const Space &) const;
bool is_superspace_of (const Space &) const;
bool is_proper_superspace_of (const Space &) const;
} ;
I take two of these objects, say, s1, and s2, plus a relation r,
between them. I intend to test:
s1 r s2; // True or False?
I store the relation r and s2 together as a relational predicate rp. I
can then apply rp to any subject s1 to see if rp complements s1:
int main ()
{
Space s1, s2;
Relational_Predicate<Space> rp(&Space::is_proper_subspace_of, s2);
if (rp.complements(s1))
cout << "s1 is proper subspace of s2";
else
cout << "s1 is not proper subspace of s2";
return 0;
}
There are many places in my project where I need to do this, where I
have a class that closely resembles Space in form - it is concrete and
contains 10-20 member function with identical signatures, each const,
taking by const reference one of its kind and returning bool. The
member functions are typically a combination of traditonal relational
operators (==, !=, >, <, etc.), plus similar functions written in text
(when there are not enough overloadable C++ operators). As mentioned
earlier, I need to store the relational predicates in containers, so a
comparison < between two Relational_Predicate<>'s is necessary.
Right now, I have a static table of pointers to member functions that
is declared by Relational_Predicate<>, but defined by the code for the
class {} argument to Relational_Predicate. From this table, I can
find the index of a member function of class {}, and when I need to
compare two Relational_Predicate<> objects, I compare subject of the
relational predicate directly, but for the relation, I compare not the
relation itself but the index of the relation as determined from the
table. This method is stable accross platforms since the table is
constructed by the library designer (me). But for speed, this method
requires that the index be cached in each Relational_Predicate<>
object, an overhead of say 1 or 2 bytes. Otherwise, during comparison
inside a container, a lookup into the table will be necessary each
time, which turns out to be unacceptable for my project.
Your note about member functions possibly being struct's makes me
almost certain that I will avoid memcmp tricks on the pointer and just
use the cached index method.
-Le Chaud Lapin-
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]