Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template temporal value

Reply
Thread Tools

Template temporal value

 
 
jose luis fernandez diaz
Guest
Posts: n/a
 
      08-03-2004
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-03-2004
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
 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      08-03-2004
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
 
Reply With Quote
 
jose luis fernandez diaz
Guest
Posts: n/a
 
      08-04-2004
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:<. 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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-04-2004
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:<. 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Temporal Lobe Frustrations!..any advice? SSRI's =?utf-8?Q?Ctrl=C2=A4/Alt=C2=A4/Del=C2=A4?= Computer Support 1 10-28-2006 04:00 AM
To be happy in the temporal world and in the eternal abode :) The truth Digital Photography 7 10-28-2006 01:08 AM
Compact Flash temporal life span Rich Fife Digital Photography 10 04-29-2004 06:44 PM
Verifying temporal properties. Need problems. Bjorn Heimir Bjornsson Python 5 04-06-2004 10:15 AM
Is there a temporal object created? Hongzheng Wang C++ 5 12-09-2003 05:05 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57