optimization for reference syntax as for function

G

Grizlyk

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/
 
S

Stuart Redmann

Grizlyk said:
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
 
G

Grizlyk

Stuart said:
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.


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/
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top