473,322 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Determining member order

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))
cout << "foo::a comes before foo::b in memory" << endl;
else
cout << "foo::b comes before foo::a in memory" << endl;

return 0;
}
Jul 19 '05 #1
1 1210
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

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jason Heyes | last post by:
Here is a program that doesn't work as intended: #include <iostream> using namespace std; class MyClass { int bar; int foo; public:
3
by: Lowen | last post by:
It's my understanding a static member variable will be one instance among all objects created from it, and static member functions only access static variables. When coding, I am still not sure how...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
0
by: Robert Potthast | last post by:
Hello, I want to make my garbage collector more safe. To make it more safe I need to know if an object has been allocated on the stack or on the heap using the operator new. My garbage collector...
7
by: Jean-David Beyer | last post by:
I have six hard drives (4 SCSI and 2 EIDE) on my main machine with parts of a database on each drive. The main index is on one SCSI drive all to itself. The main data are on the other three SCSI...
2
by: msnews.microsoft.com | last post by:
Hello, I have the scenario. I m building an application either in asp.net or window application. This application is base on n-tier application model. Let us take example of Northwind Database in...
8
by: vord.fok | last post by:
How do you keep a value in a member function ? i mean ... void main () { int a =0; change(a); change(a);
16
by: Fir5tSight | last post by:
Hi All, I have a small C#.NET program that is as follows: using System; class A { protected int x = 123; }
4
by: lord trousers | last post by:
I'm implementing a garbage collector in C++ for a fun new language (don't ask), and I've found that it spends a good amount of time calling "finalize" on objects that are freed/not relocated. The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.