Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is it legal code?

Reply
Thread Tools

Is it legal code?

 
 
Paul
Guest
Posts: n/a
 
      02-19-2011
Is it legal to invoke a (non static) member function without an object?

I read something in the C++ standard that it may not be standard compliant
code to invoke a (non static)member function directly from a pointer.

 
Reply With Quote
 
 
 
 
itaj sherman
Guest
Posts: n/a
 
      02-19-2011
On Feb 19, 1:27*pm, "Paul" <(E-Mail Removed)> wrote:
> Is it legal to invoke a (non static) member function without an object?


I think a better question would be:
Is it legal to invoke an N-ary function, with less than N arguments?
* A defaut value is considered an argument.
* That would include the argument referred to by 'this' in non-static
member functions. A non-staic member funcion has arity 1 or bigger.

I think 5.2.2, 9.3, 9.3.1 might be relevant.

>
> I read something in the C++ standard that it may not be standard compliant
> code to invoke a (non static)member function directly from a pointer.


Which paragraph in the standard are you referring to?

itaj
 
Reply With Quote
 
 
 
 
Paul
Guest
Posts: n/a
 
      02-19-2011
I found two quotes from the standard:

"5.2.2
For a member function call, the postfix expression shall be an implicit
(9.3.1, 9.4) or explicit class member access (5.2.5) whose idexpression is a
function member name, or a pointertomember expression (5.5) selecting a
function member. The first expression in the postfix expression is then
called the object expression, and the call
is as a member of the object pointed to or referred to. In the case of an
implicit class member access, the
implied object is the one pointed to by this. [Note: a member function call
of the form f() is interpreted
as (*this).f() (see 9.3.1). ]"


"9.3.1 Nonstatic member functions [class.mfct.nonstatic]
1 A nonstatic member function may be called for an object of its class type,
or for an object of a class derived
(10) from its class type, using the class member access syntax (5.2.5,
13.3.1.1). A nonstatic member function
may also be called directly using the function call syntax (5.2.2, 13.3.1.1)
- from within the body of a member function of its class or of a class
derived from its class, or
- from a meminitializer
(12.6.2) for a constructor for its class or for a class derived from its
class.
If a nonstatic 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."



Please note the last sentence. It seems clear to me that any nonstatic
member function can *only* be called on an object.

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-19-2011

"itaj sherman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On Feb 19, 1:27 pm, "Paul" <(E-Mail Removed)> wrote:
> Is it legal to invoke a (non static) member function without an object?


I think a better question would be:
Is it legal to invoke an N-ary function, with less than N arguments?
* A defaut value is considered an argument.
* That would include the argument referred to by 'this' in non-static
member functions. A non-staic member funcion has arity 1 or bigger.

I think 5.2.2, 9.3, 9.3.1 might be relevant.

>
> I read something in the C++ standard that it may not be standard compliant
> code to invoke a (non static)member function directly from a pointer.


Which paragraph in the standard are you referring to?

.................................................. ........................

sorry indenting probs ,
see other post.

 
Reply With Quote
 
Paul N
Guest
Posts: n/a
 
      02-19-2011
On Feb 19, 12:26*pm, "Paul" <(E-Mail Removed)> wrote:
> I found two quotes from the standard:
>
> "5.2.2
> For a member function call, the postfix expression shall be an implicit
> (9.3.1, 9.4) or explicit class member access (5.2.5) whose idexpression is a
> function member name, or a pointertomember expression (5.5) selecting a
> function member. The first expression in the postfix expression is then
> called the object expression, and the call
> is as a member of the object pointed to or referred to. In the case of an
> implicit class member access, the
> implied object is the one pointed to by this. [Note: a member function call
> of the form f() is interpreted
> as (*this).f() (see 9.3.1). ]"
>
> "9.3.1 Nonstatic member functions [class.mfct.nonstatic]
> 1 A nonstatic member function may be called for an object of its class type,
> or for an object of a class derived
> (10) from its class type, using the class member access syntax (5.2.5,
> 13.3.1.1). A nonstatic member function
> may also be called directly using the function call syntax (5.2.2, 13.3.1..1)
> - from within the body of a member function of its class or of a class
> derived from its class, or
> - from a meminitializer
> (12.6.2) for a constructor for its class or for a class derived from its
> class.
> If a nonstatic 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."
>
> Please note the last sentence. It seems clear to me that any nonstatic
> member function can *only* be called on an object.


Yes, your second quote seems to answer your question exactly. The
exceptions in the quoted passage allow, for instance, one member
function to call another - in this case, if the first member function
is being called on an object of the proper type then obviously the
second one is too.

I think people have written code in the past where they cast a NULL
pointer to an object of the desired type and use that to call a member
function, but this is undefined behaviour. (Though it presumably works
for them.)

The other Paul.

 
Reply With Quote
 
itaj sherman
Guest
Posts: n/a
 
      02-19-2011
On Feb 19, 2:26*pm, "Paul" <(E-Mail Removed)> wrote:
> I found two quotes from the standard:
>
> "5.2.2
> For a member function call, the postfix expression shall be an implicit
> (9.3.1, 9.4) or explicit class member access (5.2.5) whose idexpression is a
> function member name, or a pointertomember expression (5.5) selecting a
> function member. The first expression in the postfix expression is then
> called the object expression, and the call
> is as a member of the object pointed to or referred to. In the case of an
> implicit class member access, the
> implied object is the one pointed to by this. [Note: a member function call
> of the form f() is interpreted
> as (*this).f() (see 9.3.1). ]"
>
> "9.3.1 Nonstatic member functions [class.mfct.nonstatic]
> 1 A nonstatic member function may be called for an object of its class type,
> or for an object of a class derived
> (10) from its class type, using the class member access syntax (5.2.5,
> 13.3.1.1). A nonstatic member function
> may also be called directly using the function call syntax (5.2.2, 13.3.1..1)
> - from within the body of a member function of its class or of a class
> derived from its class, or
> - from a meminitializer
> (12.6.2) for a constructor for its class or for a class derived from its
> class.
> If a nonstatic 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."
>
> Please note the last sentence. It seems clear to me that any nonstatic
> member function can *only* be called on an object.


Yeah, and the function:

void foo( X&, Y& );

can only be called on two objects (specifically instances of X and Y).
What's so special about the member case?

itaj
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-19-2011

"itaj sherman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On Feb 19, 2:26 pm, "Paul" <(E-Mail Removed)> wrote:
> I found two quotes from the standard:
>
> "5.2.2
> For a member function call, the postfix expression shall be an implicit
> (9.3.1, 9.4) or explicit class member access (5.2.5) whose idexpression is
> a
> function member name, or a pointertomember expression (5.5) selecting a
> function member. The first expression in the postfix expression is then
> called the object expression, and the call
> is as a member of the object pointed to or referred to. In the case of an
> implicit class member access, the
> implied object is the one pointed to by this. [Note: a member function
> call
> of the form f() is interpreted
> as (*this).f() (see 9.3.1). ]"
>
> "9.3.1 Nonstatic member functions [class.mfct.nonstatic]
> 1 A nonstatic member function may be called for an object of its class
> type,
> or for an object of a class derived
> (10) from its class type, using the class member access syntax (5.2.5,
> 13.3.1.1). A nonstatic member function
> may also be called directly using the function call syntax (5.2.2,
> 13.3.1.1)
> - from within the body of a member function of its class or of a class
> derived from its class, or
> - from a meminitializer
> (12.6.2) for a constructor for its class or for a class derived from its
> class.
> If a nonstatic 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."
>
> Please note the last sentence. It seems clear to me that any nonstatic
> member function can *only* be called on an object.


--Yeah, and the function:

--void foo( X&, Y& );

--can only be called on two objects (specifically instances of X and Y).
--What's so special about the member case?

My newsreader not indenting your test , so I've manually marked your text
with --.

You seem confused about the meaning of "calling a member function on an
object".
You give an example of a non member fucntion and then ask .what is so
special about member functions. I don't know where you are coming from , do
you know the difference between a member function and non member function?


 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-19-2011

"Paul N" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On Feb 19, 12:26 pm, "Paul" <(E-Mail Removed)> wrote:
> I found two quotes from the standard:
>
> "5.2.2
> For a member function call, the postfix expression shall be an implicit
> (9.3.1, 9.4) or explicit class member access (5.2.5) whose idexpression is
> a
> function member name, or a pointertomember expression (5.5) selecting a
> function member. The first expression in the postfix expression is then
> called the object expression, and the call
> is as a member of the object pointed to or referred to. In the case of an
> implicit class member access, the
> implied object is the one pointed to by this. [Note: a member function
> call
> of the form f() is interpreted
> as (*this).f() (see 9.3.1). ]"
>
> "9.3.1 Nonstatic member functions [class.mfct.nonstatic]
> 1 A nonstatic member function may be called for an object of its class
> type,
> or for an object of a class derived
> (10) from its class type, using the class member access syntax (5.2.5,
> 13.3.1.1). A nonstatic member function
> may also be called directly using the function call syntax (5.2.2,
> 13.3.1.1)
> - from within the body of a member function of its class or of a class
> derived from its class, or
> - from a meminitializer
> (12.6.2) for a constructor for its class or for a class derived from its
> class.
> If a nonstatic 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."
>
> Please note the last sentence. It seems clear to me that any nonstatic
> member function can *only* be called on an object.


--Yes, your second quote seems to answer your question exactly. The
--exceptions in the quoted passage allow, for instance, one member
--function to call another - in this case, if the first member function
--is being called on an object of the proper type then obviously the
--second one is too.

Yes I agree.

--I think people have written code in the past where they cast a NULL
--pointer to an object of the desired type and use that to call a member
--function, but this is undefined behaviour. (Though it presumably works
--for them.)

Yup I understand this, what I was trying to establish is if it's standard
complaint code. As this quote from standards suggests it it's undefined
behaviour it means its non standard compliant code.

Ty for your post.



 
Reply With Quote
 
itaj sherman
Guest
Posts: n/a
 
      02-19-2011
On Feb 19, 3:06*pm, "Paul" <(E-Mail Removed)> wrote:
> "itaj sherman" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
> On Feb 19, 2:26 pm, "Paul" <(E-Mail Removed)> wrote:
>
>
>
>
>
> > I found two quotes from the standard:

>
> > "5.2.2
> > For a member function call, the postfix expression shall be an implicit
> > (9.3.1, 9.4) or explicit class member access (5.2.5) whose idexpression is
> > a
> > function member name, or a pointertomember expression (5.5) selecting a
> > function member. The first expression in the postfix expression is then
> > called the object expression, and the call
> > is as a member of the object pointed to or referred to. In the case of an
> > implicit class member access, the
> > implied object is the one pointed to by this. [Note: a member function
> > call
> > of the form f() is interpreted
> > as (*this).f() (see 9.3.1). ]"

>
> > "9.3.1 Nonstatic member functions [class.mfct.nonstatic]
> > 1 A nonstatic member function may be called for an object of its class
> > type,
> > or for an object of a class derived
> > (10) from its class type, using the class member access syntax (5.2.5,
> > 13.3.1.1). A nonstatic member function
> > may also be called directly using the function call syntax (5.2.2,
> > 13.3.1.1)
> > - from within the body of a member function of its class or of a class
> > derived from its class, or
> > - from a meminitializer
> > (12.6.2) for a constructor for its class or for a class derived from its
> > class.
> > If a nonstatic 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."

>
> > Please note the last sentence. It seems clear to me that any nonstatic
> > member function can *only* be called on an object.

>
> --Yeah, and the function:
>
> --void foo( X&, Y& );
>
> --can only be called on two objects (specifically instances of X and Y).
> --What's so special about the member case?
>
> My newsreader not indenting your test , so I've manually marked your text
> with *--.
>
> You seem confused about the meaning of "calling a member function on an
> object".
> You give an example of a non member fucntion and then ask .what is so
> special about member functions. I don't know where you are coming from , do
> you know the difference between a member function and non member function?- Hide quoted text -
>


No.
I don't think you read carefully everything I said.

itaj
 
Reply With Quote
 
Johannes Schaub (litb)
Guest
Posts: n/a
 
      02-19-2011
Paul wrote:

> Is it legal to invoke a (non static) member function without an object?
>
> I read something in the C++ standard that it may not be standard compliant
> code to invoke a (non static)member function directly from a pointer.


It is undefined behavior if you do so.

struct A { void f() { } };
int main() { ((A*)0)->f(); }

Undefined. No object.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it legal to write an logical equation for a FPGA LUT in claims of a patent? Weng Tianxiang VHDL 12 12-10-2005 03:49 PM
Research: File-sharers big legal download spenders. Silverstrand Front Page News 0 07-27-2005 03:00 PM
State machine transition on internal signals - is it legal? Divyang M VHDL 9 05-18-2005 03:58 PM
State machine transition on internal signals -- is it legal? Divyang M VHDL 1 05-15-2005 09:36 AM
Is this legal? Valentin Tihomirov VHDL 20 10-29-2003 10:31 AM



Advertisments