473,473 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What's the standard say about this code?

#include <cassert>

class Foo {
public:
virtual void fnA() = 0;
virtual void fnB() = 0;
};

int main() {
assert( &Foo::fnB );
assert( &Foo::fnA );
}

What does the standard say about the above code? In the compiler I'm
using now, the first assert will not fire, but the second one will. I
expected that neither assert would fire...

Sep 30 '08 #1
8 1619
On Sep 30, 4:40*pm, "Daniel T." <danie...@earthlink.netwrote:
#include <cassert>

class Foo {
public:
* * * * virtual void fnA() = 0;
* * * * virtual void fnB() = 0;

};

int main() {
* * * * assert( &Foo::fnB );
* * * * assert( &Foo::fnA );

}

What does the standard say about the above code? In the compiler I'm
using now, the first assert will not fire, but the second one will. I
expected that neither assert would fire...
Are you sure that the second assert is failing?

Both &Foo::fnB and &Foo::fnA should yield a non-NULL member function
pointer, which gets converted to bool(true) when passed into assert().

--
Max

Sep 30 '08 #2
On Sep 30, 11:53*am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On Sep 30, 4:40*pm, "Daniel T." <danie...@earthlink.netwrote:
#include <cassert>
class Foo {
public:
* * * * virtual void fnA() = 0;
* * * * virtual void fnB() = 0;
};
int main() {
* * * * assert( &Foo::fnB );
* * * * assert( &Foo::fnA );
}
What does the standard say about the above code? In the compiler I'm
using now, the first assert will not fire, but the second one will. I
expected that neither assert would fire...

Are you sure that the second assert is failing?

Both &Foo::fnB and &Foo::fnA should yield a non-NULL member function
pointer, which gets converted to bool(true) when passed into assert().
Yes, I am sure that the second assert is failing. If this is a
compiler bug, then I will submit it to the vendor, but I want to make
sure it actually *is* a compiler bug first.

Sep 30 '08 #3
On Sep 30, 5:40 pm, "Daniel T." <danie...@earthlink.netwrote:
#include <cassert>
class Foo {
public:
virtual void fnA() = 0;
virtual void fnB() = 0;
};
int main() {
assert( &Foo::fnB );
assert( &Foo::fnA );
}
What does the standard say about the above code?
There should be no problem with it.
In the compiler I'm using now, the first assert will not fire,
but the second one will. I expected that neither assert would
fire...
It's guaranteed by the standard. It works with the three
compilers I have access to (Sun CC, g++ and VC++), at least when
you compile in standard conformant mode. (Note that by default,
pointers to member functions do not work in VC++. You must use
the option /vmg. Not that I think that their non-conformity
otherwise would play a role here.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

Oct 1 '08 #4
James Kanze <ja*********@gmail.comwrote:
"Daniel T." <danie...@earthlink.netwrote:
#include <cassert>
class Foo {
public:
virtual void fnA() = 0;
virtual void fnB() = 0;
};
int main() {
assert( &Foo::fnB );
assert( &Foo::fnA );
}
What does the standard say about the above code?

There should be no problem with it.
In the compiler I'm using now, the first assert will not fire,
but the second one will. I expected that neither assert would
fire...

It's guaranteed by the standard. It works with the three
compilers I have access to (Sun CC, g++ and VC++), at least when
you compile in standard conformant mode. (Note that by default,
pointers to member functions do not work in VC++. You must use
the option /vmg. Not that I think that their non-conformity
otherwise would play a role here.)
Can I get chapter and verse from the standard on this? It sounds like I
need to submit a bug report to the compiler vender.

(For those who may be interested in tracking these things, I'm using
CodeWarrior Development Studio for NintendoDS.)
Oct 1 '08 #5
On 1 Okt, 12:50, "Daniel T." <danie...@earthlink.netwrote:
James Kanze <james.ka...@gmail.comwrote:
"Daniel T." <danie...@earthlink.netwrote:
#include <cassert>
class Foo {
public:
* * * * virtual void fnA() = 0;
* * * * virtual void fnB() = 0;
};
int main() {
* * * * assert( &Foo::fnB );
* * * * assert( &Foo::fnA );
}
What does the standard say about the above code?
There should be no problem with it.
In the compiler I'm using now, the first assert will not fire,
but the second one will. I expected that neither assert would
fire...
It's guaranteed by the standard. *It works with the three
compilers I have access to (Sun CC, g++ and VC++), at least when
you compile in standard conformant mode. *(Note that by default,
pointers to member functions do not work in VC++. *You must use
the option /vmg. *Not that I think that their non-conformity
otherwise would play a role here.)

Can I get chapter and verse from the standard on this? It sounds like I
need to submit a bug report to the compiler vender.
I believe it is disallowed by 4.11/1 [conv.mem]:

"A null pointer constant (4.10) can be converted to a pointer to
member type; the result is the null member pointer value of that type
and is distinguishable from any pointer to member not created from a
null pointer constant.(...)"

Therefore, an rvalue obtained by using address-of on a member shall
not yield a null pointer constant.
(For those who may be interested in tracking these things, I'm using
CodeWarrior *Development Studio for NintendoDS)
Oct 1 '08 #6
On Oct 1, 12:49*pm, Triple-DES <DenPlettf...@gmail.comwrote:
On 1 Okt, 12:50, "Daniel T." <danie...@earthlink.netwrote:
James Kanze <james.ka...@gmail.comwrote:
"Daniel T." <danie...@earthlink.netwrote:
#include <cassert>
class Foo {
public:
* * * * virtual void fnA() = 0;
* * * * virtual void fnB() = 0;
};
int main() {
* * * * assert( &Foo::fnB );
* * * * assert( &Foo::fnA );
}
What does the standard say about the above code?
There should be no problem with it.
In the compiler I'm using now, the first assert will not fire,
but the second one will. I expected that neither assert would
fire...
It's guaranteed by the standard. *It works with the three
compilers I have access to (Sun CC, g++ and VC++), at least when
you compile in standard conformant mode. *(Note that by default,
pointers to member functions do not work in VC++. *You must use
the option /vmg. *Not that I think that their non-conformity
otherwise would play a role here.)
Can I get chapter and verse from the standard on this? It sounds like I
need to submit a bug report to the compiler vender.

I believe it is disallowed by 4.11/1 [conv.mem]:

"A null pointer constant (4.10) can be converted to a pointer to
member type; the result is the null member pointer value of that type
and is distinguishable from any pointer to member not created from a
null pointer constant.(...)"

Therefore, an rvalue obtained by using address-of on a member shall
not yield a null pointer constant.
In the original question a member function pointer gets converted to
bool. Is 4.11/1 applicable here?

--
Max
Oct 1 '08 #7
On 2008-10-01 17:11, Maxim Yegorushkin wrote:
On Oct 1, 12:49 pm, Triple-DES <DenPlettf...@gmail.comwrote:
>On 1 Okt, 12:50, "Daniel T." <danie...@earthlink.netwrote:
James Kanze <james.ka...@gmail.comwrote:
"Daniel T." <danie...@earthlink.netwrote:
#include <cassert>
class Foo {
public:
virtual void fnA() = 0;
virtual void fnB() = 0;
};
int main() {
assert( &Foo::fnB );
assert( &Foo::fnA );
}
What does the standard say about the above code?
There should be no problem with it.
In the compiler I'm using now, the first assert will not fire,
but the second one will. I expected that neither assert would
fire...
It's guaranteed by the standard. It works with the three
compilers I have access to (Sun CC, g++ and VC++), at least when
you compile in standard conformant mode. (Note that by default,
pointers to member functions do not work in VC++. You must use
the option /vmg. Not that I think that their non-conformity
otherwise would play a role here.)
Can I get chapter and verse from the standard on this? It sounds like I
need to submit a bug report to the compiler vender.

I believe it is disallowed by 4.11/1 [conv.mem]:

"A null pointer constant (4.10) can be converted to a pointer to
member type; the result is the null member pointer value of that type
and is distinguishable from any pointer to member not created from a
null pointer constant.(...)"

Therefore, an rvalue obtained by using address-of on a member shall
not yield a null pointer constant.

In the original question a member function pointer gets converted to
bool. Is 4.11/1 applicable here?
No, 4.12 is probably the correct section:

"An rvalue of arithmetic, enumeration, pointer, or pointer to member
type can be converted to an rvalue of type bool. A zero value, null
pointer value, or null member pointer value is converted to false; any
other value is converted to true."

--
Erik Wikström
Oct 1 '08 #8
On 1 Okt, 19:14, Erik Wikstrm <Erik-wikst...@telia.comwrote:
On 2008-10-01 17:11, Maxim Yegorushkin wrote:


On Oct 1, 12:49 pm, Triple-DES <DenPlettf...@gmail.comwrote:
On 1 Okt, 12:50, "Daniel T." <danie...@earthlink.netwrote:
James Kanze <james.ka...@gmail.comwrote:
"Daniel T." <danie...@earthlink.netwrote:
#include <cassert>
class Foo {
public:
* * * * virtual void fnA() = 0;
* * * * virtual void fnB() = 0;
};
int main() {
* * * * assert( &Foo::fnB );
* * * * assert( &Foo::fnA );
}
What does the standard say about the above code?
There should be no problem with it.
In the compiler I'm using now, the first assert will not fire,
but the second one will. I expected that neither assert would
fire...
It's guaranteed by the standard. *It works with the three
compilers I have access to (Sun CC, g++ and VC++), at least when
you compile in standard conformant mode. *(Note that by default,
pointers to member functions do not work in VC++. *You must use
the option /vmg. *Not that I think that their non-conformity
otherwise would play a role here.)
Can I get chapter and verse from the standard on this? It sounds like I
need to submit a bug report to the compiler vender.
I believe it is disallowed by 4.11/1 [conv.mem]:
"A null pointer constant (4.10) can be converted to a pointer to
member type; the result is the null member pointer value of that type
and is distinguishable from any pointer to member not created from a
null pointer constant.(...)"
Therefore, an rvalue obtained by using address-of on a member shall
not yield a null pointer constant.
In the original question a member function pointer gets converted to
bool. Is 4.11/1 applicable here?

No, 4.12 is probably the correct section:

"An rvalue of arithmetic, enumeration, pointer, or pointer to member
type can be converted to an rvalue of type bool. A zero value, null
pointer value, or null member pointer value is converted to false; any
other value is converted to true."
Of course, but the problem is that taking the address of a function
yields a null member pointer value, not the fact that it is
subsequently converted to false. The OP might as well have written:

assert( &Foo::fnB != 0);
assert( &Foo::fnA != 0);

And 4.12 would be completely irrelevant, but the result of the second
assert would still violate 4.11/1

DP
Oct 2 '08 #9

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

Similar topics

52
by: Tony Marston | last post by:
Several months ago I started a thread with the title "What is/is not considered to be good OO programming" which started a long and interesting discussion. I have condensed the arguments into a...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
140
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
2
by: Thomas G. Marshall | last post by:
Arthur J. O'Dwyer <ajo@nospam.andrew.cmu.edu> coughed up the following: > On Thu, 1 Jul 2004, Thomas G. Marshall wrote: >> >> Aside: I've looked repeatedly in google and for some reason cannot >>...
4
by: Luke Wu | last post by:
I am just wondering what the following terms usually mean: 1) "Standard C" 2) "K&R C" 3) "ANSI C" I am pretty sure "ANSI C" usually refers to the C89 standard, but what
10
by: DJ | last post by:
What is printed to the screen? #include <stdio.h> int main(void) { printf("Hello!\n"); fclose(stdout); printf("Goodbye!\n"); return 0;
29
by: VirtualDev | last post by:
What is the future of C++?, and what is the C++0x? and is it really going to include a standard portable libraries for GUI, networking, embedded systems and so on?
30
by: albert.neu | last post by:
Hello! What is a good compiler to use? (for MS Windows, for Linux) Any recommendations?? What's the point of having a C99 standard, if different compilers still produce differing results? ...
36
by: Pat | last post by:
Hi, I've run into a strange problem, but one that seems like it might be fairly common. I have a single base class, from which several other classes are derived. To keep the example simple,...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.