Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Is it legal code?

 
 
thomas
Guest
Posts: n/a
 
      02-20-2011
On Feb 19, 7:27*pm, "Paul" <(E-Mail Removed)> 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.


Nope. You must create an object to use its members unless the member
is declared static.
 
Reply With Quote
 
 
 
 
itaj sherman
Guest
Posts: n/a
 
      02-20-2011
On Feb 20, 3:50 am, "Paul" <(E-Mail Removed)> wrote:
> "itaj sherman" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
> On Feb 19, 3:06 pm, "Paul" <(E-Mail Removed)> wrote:
>


>
> --I don't think you read carefully everything I said.
>
> No I try to but sorry please explain


You propose a few questions about nonstatic member functions. What I'm
saying is that there is no reason for these questions to be
specifically about nonstatic member function. These questions can/
should be asked about all functions the same. There is nothing about
nonstatic member functions that makes a difference with respect to
these questions.

The difference between nonstatic member functions and other functions
is in syntax and some name lookups:

call syntax:
arg0.foo( arg1, arg2 );
instead of:
foo( arg0, arg1, arg2 );

declaration syntax:
Ret Par0::foo( Par1 arg1, Par2 arg2 );
instead of:
Ret foo( Par0& arg0, Par1 arg1, Par2 arg2);

And because it's not possible by the syntax to give a name for arg0,
use a keyword 'this', which could be a reference, but was decided to
be the equivalent const pointer to the object.
So as if the function begins with:
Par0* const this = &arg0;
* Except that a temporary can bind to a hipothetic non const reference
arg0. But nobody in this thread was talking about the temporaries
difference.

Note that:
arg0->foo( arg1, arg2 );
is equivalent to:
(*arg0).foo( arg1, arg2 );

To assure some terminology:
A function of arity N, has N parameters (in CS sense). Non-static
member function, can only be of arity 1 or bigger, and their first
parameter/argument is written before the fucntion identifier (instead
of inside the braces, both declaration and call).

Now I'll show how a few of the questions could be frased in the more
general sense of functions, instead of only nonstatic member
functions:

1)
From Shaub's post:
> 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 for the same reason. It has nothing
specific to do with it being a nonstatic member function.

2)
Your first question
> Is it legal to invoke a (non static) member function without an object?


can be refrased:
Is it legal to invoke a function with a reference parameter without an
object?

3) and the grand final:

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


It follows that if an object(or derived object) does not exist then it
is
undefined behaviour pass it to a reference parameter of any function?
Therefore it must be true that a function with reference parameter
does not exist without an
object (for that parameter).

itaj
 
Reply With Quote
 
 
 
 
itaj sherman
Guest
Posts: n/a
 
      02-20-2011
I hope you misunderstood me.

On Feb 19, 8:05 pm, "Johannes Schaub (litb)"
<(E-Mail Removed)> wrote:
> itaj sherman wrote:
> > On Feb 19, 5:19 pm, "Johannes Schaub (litb)"
> > <(E-Mail Removed)> wrote:
> >> 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.


I agree, but the same thing is true for all functions, not just
nonstatic members:

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


So what do you mean by that?????????????

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

"Öö Tiib" <(E-Mail Removed)> wrote in message news:375b0c4b-0c6f-4bc8-bae4-
< snip >

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

This is where I am uncertain, the definition of 'legal code' and 'valid
code'. This seems a pretty fair
description you have given . Is this actually defined anywhere or is it just
the generally accepted definition?

Ty for your replies , I have snipped alot to make my indenting problems
easier to handle.

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

"itaj sherman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Feb 20, 3:50 am, "Paul" <(E-Mail Removed)> wrote:
>> "itaj sherman" <(E-Mail Removed)> wrote in message
>>
>> news:(E-Mail Removed)...
>> On Feb 19, 3:06 pm, "Paul" <(E-Mail Removed)> wrote:
>>

>
>>
>> --I don't think you read carefully everything I said.
>>
>> No I try to but sorry please explain

>
> You propose a few questions about nonstatic member functions. What I'm
> saying is that there is no reason for these questions to be
> specifically about nonstatic member function. These questions can/
> should be asked about all functions the same. There is nothing about
> nonstatic member functions that makes a difference with respect to
> these questions.

It seems inconceivable that a normal function is called on an object, I
don't see how this question can be asked for a normal function.
>
> The difference between nonstatic member functions and other functions
> is in syntax and some name lookups:
>
> call syntax:
> arg0.foo( arg1, arg2 );
> instead of:
> foo( arg0, arg1, arg2 );
>
> declaration syntax:
> Ret Par0::foo( Par1 arg1, Par2 arg2 );
> instead of:
> Ret foo( Par0& arg0, Par1 arg1, Par2 arg2);
>
> And because it's not possible by the syntax to give a name for arg0,
> use a keyword 'this', which could be a reference, but was decided to
> be the equivalent const pointer to the object.
> So as if the function begins with:
> Par0* const this = &arg0;
> * Except that a temporary can bind to a hipothetic non const reference
> arg0. But nobody in this thread was talking about the temporaries
> difference.
>
> Note that:
> arg0->foo( arg1, arg2 );
> is equivalent to:
> (*arg0).foo( arg1, arg2 );
>
> To assure some terminology:
> A function of arity N, has N parameters (in CS sense). Non-static
> member function, can only be of arity 1 or bigger, and their first
> parameter/argument is written before the fucntion identifier (instead
> of inside the braces, both declaration and call).
>
> Now I'll show how a few of the questions could be frased in the more
> general sense of functions, instead of only nonstatic member
> functions:
>
> 1)
> From Shaub's post:
>> 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 for the same reason. It has nothing
> specific to do with it being a nonstatic member function.
>

The above is using a null pointer of type A*, and you show that the reason
A->memb_func() fails is the reason memb_funct(A) fails when A is a null
pointer passed as a reference., yes ?
But this is not the same as:
func_pointer = A::my_memb_func;
invoke func_pointer.

So the question is not the same for normal functions and member functions.


> 2)
> Your first question
>> Is it legal to invoke a (non static) member function without an object?

>
> can be refrased:
> Is it legal to invoke a function with a reference parameter without an
> object?
>

I disagree with your conclusion that these two things are the same.

> 3) and the grand final:
>
>> 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.

>
> It follows that if an object(or derived object) does not exist then it
> is
> undefined behaviour pass it to a reference parameter of any function?
> Therefore it must be true that a function with reference parameter
> does not exist without an
> object (for that parameter).
>

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-20-2011
On Feb 20, 12:38 am, "Paul" <(E-Mail Removed)> wrote:
> "gwowen" <(E-Mail Removed)> wrote in message


> news:(E-Mail Removed)...
> On Feb 19, 11:27 am, "Paul" <(E-Mail Removed)> 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.


> --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 what the context is, but yes: pointers (including
pointers to functions, and pointers to members) are objects. In
C++; other languages may consider them differently.

> 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 presence of an object.


A non-static member cannot be called without an instance of the
class (an object). As others have pointed out, a non-static
member function has an implicit first argument; at least for
overload resolution (and I think more generally), it has type
MyClass& (or MyClass const&, if the function is const). (Inside
the function, the type of this is a pointer, but this is mainly
because member functions were added before references; in modern
C++, it would make more sense for this to have a reference
type.)

> I think object in this context is (an instance of class type),
> not purely an object in the context used within the C++
> standard.


In this case, it doesn't make a difference, since only class
types can have member functions.

--
James Kanze

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

"itaj sherman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I hope you misunderstood me.
>
> On Feb 19, 8:05 pm, "Johannes Schaub (litb)"
> <(E-Mail Removed)> wrote:
>> itaj sherman wrote:
>> > On Feb 19, 5:19 pm, "Johannes Schaub (litb)"
>> > <(E-Mail Removed)> wrote:
>> >> 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.

>
> I agree, but the same thing is true for all functions, not just
> nonstatic members:

I disagree with you see the other more detailed thread.
>
>>
>> > 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.

>
> So what do you mean by that?????????????
>

It's not a very nice thing to say as you have raised a valid argument
,whether I disagree with it or not.

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-20-2011
On Feb 20, 1:09 am, "Paul" <(E-Mail Removed)> wrote:
> "gwowen" <(E-Mail Removed)> wrote in message


> news:(E-Mail Removed)...
> On Feb 19, 11:27 am, "Paul" <(E-Mail Removed)> 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.


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


I don't quite follow you here. If I have a function:
void f(int&);
, it's undefined behavior for me to call f without a an object
of type int. The function f(int&) still "exists". For some
definition of "exists"---this is getting a bit too metaphysical
for me. One could argue that in C++, only "objects" exist, and
references and functions aren't objects, so can't "exist".

--
James Kanze
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-20-2011

>"gwowen" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>On Feb 20, 1:09 am, "Paul" <(E-Mail Removed)> wrote:
>> 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.

According to the standard you cannot have any strlen without an object of ,
or derived from , string.
That is assuming strlen is defined as a non static member function of
string, in class string.

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


Consider with a more simple example:
class C_type{
public:
void foo(){};
};

int main(){
C_type* obj1 = new C_type();
obj1.foo(); /*foo existshere*/
delete obj1;
/*foo no longer exists*/
}

If we do not have an instance of C_type then we do not have any foo()'s.
The function definition will still exist someplace so if another C_type is
created another foo can be invoked.

I aggree it is not strictly true to say the function no longer exists
because it is possible to directly call the function with a function
pointer. But since this is not valid according to the C+ standard, it can be
said , as far as the standard is concerned, that the function does not exist
unless an object of the class type exists.



 
Reply With Quote
 
itaj sherman
Guest
Posts: n/a
 
      02-20-2011
On Feb 20, 5:07 pm, "Paul" <(E-Mail Removed)> wrote:
> "itaj sherman" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > On Feb 20, 3:50 am, "Paul" <(E-Mail Removed)> wrote:
> >> "itaj sherman" <(E-Mail Removed)> wrote in message

>
> >>news:(E-Mail Removed)...
> >> On Feb 19, 3:06 pm, "Paul" <(E-Mail Removed)> wrote:

>
> >> --I don't think you read carefully everything I said.

>
> >> No I try to but sorry please explain

>
> > You propose a few questions about nonstatic member functions. What I'm
> > saying is that there is no reason for these questions to be
> > specifically about nonstatic member function. These questions can/
> > should be asked about all functions the same. There is nothing about
> > nonstatic member functions that makes a difference with respect to
> > these questions.

>
> It seems inconceivable that a normal function is called on an object, I
> don't see how this question can be asked for a normal function.
>


This is what I tried to explain. You say "called on an object" instead
"pass an object argument to the first parameter", but they mean the
exact same thing.

>
> > Now I'll show how a few of the questions could be frased in the more
> > general sense of functions, instead of only nonstatic member
> > functions:

>
> > 1)
> > From Shaub's post:
> >> 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 for the same reason. It has nothing
> > specific to do with it being a nonstatic member function.

>
> The above is using a null pointer of type A*, and you show that the reason
> A->memb_func() fails is the reason memb_funct(A) fails when A is a null
> pointer passed as a reference., yes ?


Yes.
And if you understand this, I think you should understand the rest of
what I said, if you read it carfully.

> But this is not the same as:
> func_pointer = A::my_memb_func;
> invoke func_pointer.


What is "func_pointer", and what is "invoke func_pointer"?
If you invoke a function (even through a pointer) you have to pass
arguments to all parameters, and you should state how you do it here.
That includes the special argument in case of a nonstatic member
function

>
> So the question is not the same for normal functions and member functions.
>


Which question?!
All the ones you talked about are.
Syntactic and name lookups are different.

> > 2)
> > Your first question
> >> Is it legal to invoke a (non static) member function without an object?

>
> > can be refrased:
> > Is it legal to invoke a function with a reference parameter without an
> > object?

>
> I disagree with your conclusion that these two things are the same.
>


I know you disagreed. This is exactly why I tried to explain how they
are the same, with respect to your issue.
I tried my best to explain. I think if you were able to grasp this
concept in a general way (instead just nonstatic members), it would
help you clear the confusions for you and everyone else.

itaj
 
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