cpp_weenie <cp*@weenie.com> wrote in message
news:vo************@news.supernews.com...
Hello group,
Should the program below be valid? Or should it generate a compilation
error? Any references to the Standard would be appreciated...
Thanks!
#include <iostream>
using namespace std;
struct foo
{
int a;
int b;
};
template<typename T1, typename T2, typename T3>
bool before(T2 T1::*mem1, T3 T1::*mem2)
{
return mem1 < mem2;
}
int main()
{
if (before(&foo::a, &foo::b))
Since you can't have an array of members of foo, I would expect a
compilation error (and, in fact, I get one). The '<' operator for addresses
only makes sense when those addresses belong to the same array.
int a[10];
int *pa = &a[0];
int *pb = &a[5];
++pa; // okay
--pb; // okay
bool ba = pa < pb; // okay
int j = pb - pa; // okay
These are all okay because an int * can point to anywhere within the same
int array.
int foo::*pf = &foo::a;
int foo::*pg = &foo::b;
++pf; // error
--pg; // error
bool bf = pf < pg; // error
int k = pg - pf; // error
The same is not true of pointers to members. It simply doesn't make sense to
compare two of them.
I don't have any standard references.
DW