optimization for reference syntax as for function

Discussion in 'C++' started by Grizlyk, Mar 12, 2007.

  1. Grizlyk

    Grizlyk Guest

    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/
     
    Grizlyk, Mar 12, 2007
    #1
    1. Advertising

  2. 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
     
    Stuart Redmann, Mar 12, 2007
    #2
    1. Advertising

  3. Grizlyk

    Grizlyk Guest

    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/
     
    Grizlyk, Mar 13, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denis Remezov
    Replies:
    20
    Views:
    738
    Jakob Bieling
    Apr 20, 2004
  2. pt
    Replies:
    8
    Views:
    802
  3. Jacek Generowicz

    Function application optimization.

    Jacek Generowicz, Dec 12, 2003, in forum: Python
    Replies:
    8
    Views:
    337
    Peter Otten
    Dec 12, 2003
  4. Replies:
    1
    Views:
    4,271
    Robert Kern
    Sep 20, 2006
  5. Ravikiran

    Zero Optimization and Sign Optimization???

    Ravikiran, Nov 17, 2008, in forum: C Programming
    Replies:
    22
    Views:
    904
    Thad Smith
    Nov 24, 2008
Loading...

Share This Page