Is it legal code?

P

Paul

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.
 
I

itaj sherman

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
 
P

Paul

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.
 
P

Paul

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.
 
P

Paul N

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.
 
I

itaj sherman

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
 
P

Paul

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?
 
P

Paul

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.
 
I

itaj sherman

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
 
J

Johannes Schaub (litb)

Paul said:
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.
 
I

itaj sherman

It is undefined behavior if you do so.

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

Undefined. No object.

struct A {};
void foo( A& );
int main() { foo( *((A*)0) ); }

This is undefined behaviour just as well.
There's nothing in this issue that has specifically to do with
nonstatic member functions.

itaj
 
G

gwowen

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.

If the pointer points to an object that still exists, its fine.
Otherwise, no. Essentially the same as dereferencing the pointer.
 
J

Johannes Schaub (litb)

itaj said:
struct A {};
void foo( A& );
int main() { foo( *((A*)0) ); }

This is undefined behaviour just as well.
There's nothing in this issue that has specifically to do with
nonstatic member functions.

Yes, please act in a troll-way.
 
P

Paul

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.

--If the pointer points to an object that still exists, its fine.
--Otherwise, no. Essentially the same as dereferencing the pointer.

Firstly sorry ABOUT INDENTING PROBS.

Is it the case that a function pointer can be an object becuse its not
actually a function , its a pointer.?
I'm not sure it helps the situation anyway because what I was trying to
deduce was whether or not a (nonstatic)member function is allowed to be
called without the presencce of an object.
I think object in this context is ( an instance of class type) , not purely
an object in the context used within the C++ standard.
 
P

Paul

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.

--If the pointer points to an object that still exists, its fine.
--Otherwise, no. Essentially the same as dereferencing the pointer.

So given that the following is true:

"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."

It follows that if an object(or derived object) does not exist then it is
undefined behaviour to call its respective nonstatic member function?
Therefore it must be true that a member function does not exist without an
object.
 
P

Paul

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.

No I try to but sorry please explain
 
Ö

Öö Tiib

--If the pointer points to an object that still exists, its fine.
--Otherwise, no. Essentially the same as dereferencing the pointer.

So given that the following is true:

"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."

It follows that if an object(or derived object) does not exist then it is
undefined behaviour to call its respective nonstatic member function?
Therefore it must be true that a member function does not exist without an
object.

Nope. What follows is that A) Member function is available for calling
regardless if any objects exist. B) In C++ language it is possible to
call member function on something that is not really proper object of
correct type. C) The behavior resulting by such wrong call is
undefined, because good people do not do it.
 
P

Paul

--If the pointer points to an object that still exists, its fine.
--Otherwise, no. Essentially the same as dereferencing the pointer.

So given that the following is true:

"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."

It follows that if an object(or derived object) does not exist then it is
undefined behaviour to call its respective nonstatic member function?
Therefore it must be true that a member function does not exist without an
object.

--Nope. What follows is that A) Member function is available for calling
--regardless if any objects exist.

But this is non standard. And also not guaranteed because if we get non
standard it is possible that I overwrite the memory where the function
defintion was. So it's a very incorrect statement to make is it not?
..
-- B) In C++ language it is possible to
--call member function on something that is not really proper object of
--correct type.
-- C) The behavior resulting by such wrong call is
--undefined, because good people do not do it.

Your B and C seems to be regarding the same point. What you seem to be
saying is that its possible to call a member function without an
object(which we already know), but it is incorrect to do so. Does this mean
you are saying it would be invalid standard C++ code?
 
Ö

Öö Tiib

--Nope. What follows is that A) Member function is available for calling
--regardless if any objects exist.

But this is non standard. And also not guaranteed because if we get non
standard it is possible that I overwrite the memory where the function
defintion was. So it's a very incorrect statement to make is it not?
.

Any attempts to access member functions as storage in memory that you
can overwrite with something are, yes, non standard. In reality the
function may exist somewhere in memory as bytes, but C++ language does
not handle it so. In some system the executable code resides in read-
only memory and in others the data resides in non-executable
memory ... so it is highly platform-specific.
-- B) In C++ language it is possible to
--call member function on something that is not really proper object of
--correct type.
-- C) The behavior resulting by such wrong call is
--undefined, because good people do not do it.

Your B and C seems to be regarding the same point. What you seem to be
saying  is that its possible to call a member function without an
object(which we already know), but it is incorrect to do so. Does this mean
you are saying it would be invalid standard C++ code?

Yes. It will be compiled by standard-compliant compiler so it is legal
code and it will run with undefined results so it is invalid code.
 
G

gwowen

It follows that if an object(or derived object) does not exist then it is
undefined behaviour to call its respective nonstatic member function?
Therefore it must be true that a member function does not exist without an
object.

No. That is simply not a syllogism. "X is undefined behaviour" is not
equivalent to "The constituent parts of X do not exist."

It is undefined behaviour to call strlen(const char *) on a pointer
that does not point to a valid NULL terminated string. That does not
mean that the strlen() function does not exist until a valid string
exists, and that it ceases to exist when all such strings go out of
scope.

Setting aside the peculiar ontological implications of such a
deduction, you can (for example) take the address of "strlen()".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top