473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Template temporal value

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
4 1167
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Hongzheng Wang | last post by:
Hi, If I have such codes: class fred { //user defined constructors/destructor here }; int main() {
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
11
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual class 'C', but only because a trait tell us so: ...
10
by: mast2as | last post by:
Is it possible to limit a template class to certain types only. I found a few things on the net but nothing seems to apply at compile time. template <typename T> class AClass { public:...
13
by: Focca | last post by:
I have a template function specialized with some base types: template <typename T> void print(const T &value); template <> void print<bool>(const bool &value) { // ... }
28
by: Neo Geshel | last post by:
NOTE: PAST EXPERIENCE HAS SHOWN ME THAT MANY ON USENET FAIL TO READ ARTICLES PROPERLY PRIOR TO ANSWERING. I AM LOOKING FOR VERY SPECIFIC INFORMATION, THEREFORE PLEASE READ AND UNDERSTAND...
9
by: Leo jay | last post by:
i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));...
0
by: David | last post by:
On Wed, Jun 18, 2008 at 11:16 AM, M.-A. Lemburg <mal@egenix.comwrote: Thanks for your reply. How do you maintain foreign key references with this approach? eg, you have these 4 tables: ...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.