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

Template temporal value

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Jul 22 '05 #2

P: n/a
On 3 Aug 2004 07:46:42 -0700, jo**********************@yahoo.es (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
Jul 22 '05 #3

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

jo**********************@yahoo.es (jose luis fernandez diaz) wrote in message news:<c2**************************@posting.google. com>...
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.

Jul 22 '05 #4

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

jo**********************@yahoo.es (jose luis fernandez diaz) wrote in message news:<c2**************************@posting.google. com>...
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
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.