Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > optimization for reference syntax as for function

Reply
Thread Tools

optimization for reference syntax as for function

 
 
Grizlyk
Guest
Posts: n/a
 
      03-12-2007
Hello.

Can compiler garantee equal optimization in the following example for
reference "named_ref" as for "named_function"? How I can declare that the
"named_ref" always will returns "*this"?

// ***
class A
{
public:
A &named_ref;
A &named_function(){ return *this; }

A():named_ref(*this){}
};

// ***
A a;

A& foo()
{
return a.named_ref;
}

A& boo()
{
return a.named_function();
}


--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/


 
Reply With Quote
 
 
 
 
Stuart Redmann
Guest
Posts: n/a
 
      03-12-2007
Grizlyk wrote:
> Hello.
>
> Can compiler garantee equal optimization in the following example for
> reference "named_ref" as for "named_function"?


This depends on the compiler you are using. As compiler issues are of no
concern in this newsgroup, you'll have to ask in a newsgroup dedicated
to the compiler you're using.

> How I can declare that the
> "named_ref" always will returns "*this"?


Declare it as const reference:
const A &named_ref;
Having done this, there is no need to define named_ref at all, as you
can write "*this" instead of named_ref.

> // ***
> class A
> {
> public:
> A &named_ref;
> A &named_function(){ return *this; }
>
> A():named_ref(*this){}
> };


Regards,
Stuart
 
Reply With Quote
 
 
 
 
Grizlyk
Guest
Posts: n/a
 
      03-13-2007
Stuart Redmann wrote:
>>
>> Can compiler garantee equal optimization in the following example for
>> reference "named_ref" as for "named_function"?

>
> This depends on the compiler you are using. As compiler issues are of no
> concern in this newsgroup, you'll have to ask in a newsgroup dedicated to
> the compiler you're using.
>
>> How I can declare that the "named_ref" always will returns "*this"?

>
> Declare it as const reference:
> const A &named_ref;



No, "const A &named_ref" is other beast, because we need "non-const A"
returned. Reference is already "const" in the sense that (unlike pointer)
any reference can not be changed after object has been created and reference
linked, but unlike to "enum", reference can be linked with different objects
during object creation.

> Having done this, there is no need to define named_ref at all, as you can
> write "*this" instead of named_ref.


Also we can not allow to use "*this" for client of the class, because we do
not know what address will be hidden by the interface name "named_ref" in
concrete implementation. I need reference syntax, but as well optimal as
inline function.

There are differences here:

A & tmp=named_function();
can be implemented as
A & tmp=*this;
//movl this,%eax

A & tmp=named_ref;
will be implemented as
A & tmp=this->named_ref;
//movl this,%eax
//movl named_ref(%eax),%eax

So we have extra memory read, but "named_ref(%eax)" == "%eax".

In other words, i want to tell to compiler, that for the concrete class the
"named_ref" will always has the concrete declared value, the "named_ref"
never will be used as runtime value and does not need memory. It is
something like "enum".

class A
{
public:
A &named_ref=*this;
};

class B
{
A a;
public:
A &named_ref=a.named_ref;
};

--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/


 
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
Zero Optimization and Sign Optimization??? Ravikiran C Programming 22 11-24-2008 03:19 AM
SciPy Optimization syntax tkpmep@hotmail.com Python 1 09-20-2006 07:54 PM
lifetime of temporary object from function return & optimization pt C++ 8 06-01-2005 01:11 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Q: Return value optimization thru several function calls? Denis Remezov C++ 20 04-20-2004 04:15 PM



Advertisments