Template temporal value

Discussion in 'C++' started by jose luis fernandez diaz, Aug 3, 2004.

  1. Hi,

    In the next class:

    template<class T> class Set_controller
    {
    Set<T>* rep;
    Lock lock;
    public:
    void insert(T* p) { Lock_ptr x(lock); rep->insert(p); }
    void remove(T* p) { Lock_ptr x(lock); rep->remove(p); }

    int is_member(T *p) { return rep->is_member(p); }

    T get_first() { T* p=rep->first(); return p ? *p : T(); }
    T get_next() { T* p=rep->next(); return p ? *p : T(); }

    T first() { Lock_ptr x(lock); T tmp = *rep->first(); return tmp; }
    T next() { Lock_ptr x(lock); T tmp = *rep->next(); return tmp; }


    // . . .

    };



    What is the difference between:

    T first() { Lock_ptr x(lock); T tmp = *rep->first(); return tmp; }

    and

    T first() { Lock_ptr x(lock); return *rep->first(); }

    Thanks,
    Jose Luis.
     
    jose luis fernandez diaz, Aug 3, 2004
    #1
    1. Advertising

  2. jose luis fernandez diaz wrote:
    > In the next class:
    >
    > template<class T> class Set_controller
    > {
    > Set<T>* rep;
    > Lock lock;
    > public:
    > void insert(T* p) { Lock_ptr x(lock); rep->insert(p); }
    > void remove(T* p) { Lock_ptr x(lock); rep->remove(p); }
    >
    > int is_member(T *p) { return rep->is_member(p); }
    >
    > T get_first() { T* p=rep->first(); return p ? *p : T(); }
    > T get_next() { T* p=rep->next(); return p ? *p : T(); }
    >
    > T first() { Lock_ptr x(lock); T tmp = *rep->first(); return tmp; }
    > T next() { Lock_ptr x(lock); T tmp = *rep->next(); return tmp; }
    >
    >
    > // . . .
    >
    > };
    >
    >
    >
    > What is the difference between:
    >
    > T first() { Lock_ptr x(lock); T tmp = *rep->first(); return tmp; }
    >
    > and
    >
    > T first() { Lock_ptr x(lock); return *rep->first(); }


    The latter is about a dozen characters shorter.

    Victor
     
    Victor Bazarov, Aug 3, 2004
    #2
    1. Advertising

  3. jose luis fernandez diaz

    tom_usenet Guest

    On 3 Aug 2004 07:46:42 -0700, (jose
    luis fernandez diaz) wrote:

    >What is the difference between:
    >
    > T first() { Lock_ptr x(lock); T tmp = *rep->first(); return tmp; }
    >
    >and
    >
    > T first() { Lock_ptr x(lock); return *rep->first(); }


    If your compiler doesn't implement the NRVO, the second is probably
    more efficient since a copy/destruct is avoided. Apart from that, not
    much, since x lives until after the return value has been constructed.

    Tom
     
    tom_usenet, Aug 3, 2004
    #3
  4. Hi,

    This code is extracted from "The C++ Programming Language"
    Stroustrup's book. It is strange to me that Strousptrup writes useless
    code, but it seems that that is the case.

    Regards,
    Jose Luis.

    (jose luis fernandez diaz) wrote in message news:<>...
    > Hi,
    >
    > In the next class:
    >
    > template<class T> class Set_controller
    > {
    > Set<T>* rep;
    > Lock lock;
    > public:
    > void insert(T* p) { Lock_ptr x(lock); rep->insert(p); }
    > void remove(T* p) { Lock_ptr x(lock); rep->remove(p); }
    >
    > int is_member(T *p) { return rep->is_member(p); }
    >
    > T get_first() { T* p=rep->first(); return p ? *p : T(); }
    > T get_next() { T* p=rep->next(); return p ? *p : T(); }
    >
    > T first() { Lock_ptr x(lock); T tmp = *rep->first(); return tmp; }
    > T next() { Lock_ptr x(lock); T tmp = *rep->next(); return tmp; }
    >
    >
    > // . . .
    >
    > };
    >
    >
    >
    > What is the difference between:
    >
    > T first() { Lock_ptr x(lock); T tmp = *rep->first(); return tmp; }
    >
    > and
    >
    > T first() { Lock_ptr x(lock); return *rep->first(); }
    >
    > Thanks,
    > Jose Luis.
     
    jose luis fernandez diaz, Aug 4, 2004
    #4
  5. jose luis fernandez diaz wrote:
    > This code is extracted from "The C++ Programming Language"
    > Stroustrup's book. It is strange to me that Strousptrup writes useless
    > code, but it seems that that is the case.


    It's not "useless code". When debugging temporary variables help quite
    significantly. Once you figured that the constructors and copying are
    all working correctly, you can get rid of "unnecessary" objects, but
    that usually takes time without any added benefit to the code itself.

    I hope that you eventually become a published author in some area and
    never make "mistakes" like the one you decided to point out today. Once
    you figured out how not to top-post, that is.

    V

    >
    > Regards,
    > Jose Luis.
    >
    > (jose luis fernandez diaz) wrote in message news:<>...
    >
    >>Hi,
    >>
    >>In the next class:
    >>
    >>template<class T> class Set_controller
    >>{
    >> Set<T>* rep;
    >> Lock lock;
    >>public:
    >> void insert(T* p) { Lock_ptr x(lock); rep->insert(p); }
    >> void remove(T* p) { Lock_ptr x(lock); rep->remove(p); }
    >>
    >> int is_member(T *p) { return rep->is_member(p); }
    >>
    >> T get_first() { T* p=rep->first(); return p ? *p : T(); }
    >> T get_next() { T* p=rep->next(); return p ? *p : T(); }
    >>
    >> T first() { Lock_ptr x(lock); T tmp = *rep->first(); return tmp; }
    >> T next() { Lock_ptr x(lock); T tmp = *rep->next(); return tmp; }
    >>
    >>
    >> // . . .
    >>
    >>};
    >>
    >>
    >>
    >>What is the difference between:
    >>
    >> T first() { Lock_ptr x(lock); T tmp = *rep->first(); return tmp; }
    >>
    >>and
    >>
    >> T first() { Lock_ptr x(lock); return *rep->first(); }
    >>
    >>Thanks,
    >>Jose Luis.



    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Aug 4, 2004
    #5
    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. Chris Theis
    Replies:
    2
    Views:
    503
    Chris Theis
    Jul 24, 2003
  2. Hongzheng Wang

    Is there a temporal object created?

    Hongzheng Wang, Dec 8, 2003, in forum: C++
    Replies:
    5
    Views:
    392
    Rob Williscroft
    Dec 9, 2003
  3. Bjorn Heimir Bjornsson

    Verifying temporal properties. Need problems.

    Bjorn Heimir Bjornsson, Apr 1, 2004, in forum: Python
    Replies:
    5
    Views:
    317
    Yermat
    Apr 6, 2004
  4. David
    Replies:
    0
    Views:
    892
    David
    Jun 18, 2008
  5. aitor

    Adding temporal role

    aitor, Sep 30, 2008, in forum: ASP .Net Security
    Replies:
    1
    Views:
    712
    Alexey Smirnov
    Oct 1, 2008
Loading...

Share This Page