473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

optimization for reference syntax as for function

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/
Mar 12 '07 #1
2 1799
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
Mar 12 '07 #2
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/
Mar 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Jakob Bieling | last post by:
Hi! I am using VC++ 7.1 and have a question about return value optimization. Consider the following code: #include <list> #include <string> struct test {
2
by: Eugene | last post by:
I am trying to set query optimization class in a simple SQL UDF like this: CREATE FUNCTION udftest ( in_item_id INT ) SPECIFIC udftest MODIFIES SQL DATA RETURNS TABLE( location_id INT,...
14
by: joshc | last post by:
I'm writing some C to be used in an embedded environment and the code needs to be optimized. I have a question about optimizing compilers in general. I'm using GCC for the workstation and Diab...
1
by: tkpmep | last post by:
I'm trying to optimize a function using SciPy's optimize.fmin, but am clearly getting the syntax wrong, and would be grateful for some guiidance. First, here's the function def func(Y,x): """Y...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
18
by: terminator(jam) | last post by:
consider: struct memory_pig{//a really large type: memory_pig(){ std::cout<<"mem pig default\n"; //etc... }; memory_pig(memory_pig const&){
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.