By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

optimization for reference syntax as for function

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.