Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-11-2008, 01:38 PM
rkldabs@gmail.com
 
Posts: n/a
Non static members

Hi,

$9.3.1/2 (N2691) states "If a non-static member function of a class X
is called for an object that is not of type X, or of a type derived
from X, the behavior is undefined."

I tried the following code

struct A{
virtual void f(){}
};

struct B : A{
void f(){}
};

int main(){
void (B::*pf)() = &B::f;
A *a;
(a->*pf)();
}

After trying several variations, I am not able to proceed beyond
getting a compilation error in VS2008. It appears to me that trying to
attempt such a code leads to an ill-formed program. I am unable to
simulate a code which would lead to "undefined" behaviour.

Does undefined behaviour always mean that the code will at least
compile/link and form an executable?

Dabs.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #2 (permalink)  
Old 08-11-2008, 11:58 PM
Ulrich Eckhardt
 
Posts: n/a
Re: Non static members

rkldabs@gmail.com wrote:
> $9.3.1/2 (N2691) states "If a non-static member function of a class X
> is called for an object that is not of type X, or of a type derived
> from X, the behavior is undefined."
>
> I tried the following code


[snip]

You're thinking too complicated:

struct A
{};
struct B: A
{
void foo() {}
};

int main() {
A a;
// Note: this upcast is wrong because 'a' is not a 'B'
B& b = static_cast<B&>(a);
// ..therefore this call yields UB
b.foo();
}

> Does undefined behaviour always mean that the code will at least
> compile/link and form an executable?


Always, no. Typically, yes. There are few cases where compilers simply
refuse to compile code that can only yield UB, the only example I'm aware
of now is a function that has a returnvalue but under no circumstance
returns anything and also doesn't throw or terminate.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #3 (permalink)  
Old 08-11-2008, 11:58 PM
Tonni Tielens
 
Posts: n/a
Re: Non static members

On Aug 11, 2:38 pm, rkld...@gmail.com wrote:
> Hi,
>
> $9.3.1/2 (N2691) states "If a non-static member function of a class X
> is called for an object that is not of type X, or of a type derived
> from X, the behavior is undefined."
>
> I tried the following code
>
> struct A{
> virtual void f(){}
> };
>
> struct B : A{
> void f(){}
> };
>
> int main(){
> void (B::*pf)() = &B::f;
> A *a;
> (a->*pf)();
> }
>
> After trying several variations, I am not able to proceed beyond
> getting a compilation error in VS2008. It appears to me that trying to
> attempt such a code leads to an ill-formed program. I am unable to
> simulate a code which would lead to "undefined" behaviour.
>
> Does undefined behaviour always mean that the code will at least
> compile/link and form an executable?


{ Edits: removed quoted signature and clc++m banner. The banner is appended
automatically to every article, including this one, and does not need to be
quoted. -mod }

Aren't you doing the opposite? You're trying to call a function of a
derived type on an object that is more generic. pf points to a
function of B and you're trying to call it on an object of A.

The following code compiles in VC6:

struct A
{
virtual void f(){}
};

struct B : A
{
void f(){}
};

int main(int argc, char* argv[])
{
void (A::*pf)() = &A::f;
B *b;
(b->*pf)();

return 0;
}


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #4 (permalink)  
Old 08-12-2008, 09:33 PM
Roman.Perepelitsa@gmail.com
 
Posts: n/a
Re: Non static members

On 12 Aug, 00:58, Ulrich Eckhardt <eckha...@satorlaser.com> wrote:
> You're thinking too complicated:
>
> struct A
> {};
> struct B: A
> {
> void foo() {}
>
> };
>
> int main() {
> A a;
> // Note: this upcast is wrong because 'a' is not a 'B'
> B& b = static_cast<B&>(a);


I believe this static_cast yields UB, therefore you can't
get UB on the next line.

5.2.9 Static cast
5 An lvalue of type “cv1 B”, where B is a class type, can
be cast to type “reference to cv2 D”, where D is a class
derived (clause 10) from B, if a valid standard conversion
from “pointer to D” to “pointer to B” exists(4.10), cv2
is the same cv-qualification as, or greater
cv-qualification than, cv1, and B is not a virtual base
class of D. The result is an lvalue of type “cv2 D.”
If the lvalue of type “cv1 B” is actually a sub-object of
an object of type D, the lvalue refers to the enclosing
object of type D. Otherwise, the result of the cast is
undefined.

It says that result of this static_cast is undefined.
Is it the same as undefined behavior? Or does it only
lead to undefined behavior if result is actually used?

> // ..therefore this call yields UB
> b.foo();
>
> Always, no. Typically, yes. There are few cases where compilers simply
> refuse to compile code that can only yield UB, the only example I'm aware
> of now is a function that has a returnvalue but under no circumstance
> returns anything and also doesn't throw or terminate.


Roman Perepelitsa.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #5 (permalink)  
Old 08-21-2008, 02:28 PM
ta0kira@yahoo.com
 
Posts: n/a
Re: Non static members

On Aug 12, 4:33 pm, "Roman.Perepeli...@gmail.com"
<Roman.Perepeli...@gmail.com> wrote:

> I believe this static_cast yields UB, therefore you can't
> get UB on the next line.


If the program still executes past the 'static_cast' then the
reference is the result of UB and is *obviously not* a valid 'B'
object except in the furthest bastardizations of UB, therefore making
the example valid in pretty much all cases. As far as I know, UB
doesn't necessarily mean that "the program must degrade into mayhem
from that point forward." I believe the rule is meant to apply free
of the contexts required to get to the point in which it's applicable.
Kevin P. Barry

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 05:56 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Xecuter 3 Mod Chip | Electricity Suppliers | Internet Advertising | Credit Card Consolidation | Mortgage Calculator



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114