Template temporal value

  • Thread starter jose luis fernandez diaz
  • Start date
J

jose luis fernandez diaz

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.
 
V

Victor Bazarov

jose said:
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
 
T

tom_usenet

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
 
J

jose luis fernandez diaz

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.
 
V

Victor Bazarov

jose said:
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
 

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

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top