Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Defs.h minmax() change/fix
Defs.h minmax() change/fix [message #24061] Wed, 23 December 2009 19:31 Go to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
hi there

in Defs.h:214

why not using

template <class T>
inline const T& minmax(const T& x, const T& _min, const T& _max) { return min(max(x, _min), _max); }


instead of

template <class T>
inline T minmax(T x, T _min, T _max) { return min(max(x, _min), _max); }


which is sort of logically semantics
especially when you consider the other related templates:

template <class T> inline const T& min(const T& a, const T& b) { return a < b ? a : b; }
template <class T> inline const T& max(const T& a, const T& b) { return a > b ? a : b; }

Re: Defs.h minmax() change/fix [message #24069 is a reply to message #24061] Thu, 24 December 2009 10:58 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
kohait00 wrote on Wed, 23 December 2009 13:31

hi there

in Defs.h:214

why not using

template <class T>
inline const T& minmax(const T& x, const T& _min, const T& _max) { return min(max(x, _min), _max); }


instead of

template <class T>
inline T minmax(T x, T _min, T _max) { return min(max(x, _min), _max); }


which is sort of logically semantics
especially when you consider the other related templates:

template <class T> inline const T& min(const T& a, const T& b) { return a < b ? a : b; }
template <class T> inline const T& max(const T& a, const T& b) { return a > b ? a : b; }




What if min / max for T is defined returning temporary?

Foo min(const Foo& a, const Foo& b);

then you would be returning reference to temporary....

Well, maybe this is not really strong argument as min/max are rarely defined directly - OTOH compilers are really goot at optimizing, so practical differences in code generated are, I believe, unlikely.

That said, proposed change would be nice if type does not have copy.

I am sort of undecided, but current minmax worked fine for 10 years, I guess there is not a strong incentive to change now.

Mirek
Re: Defs.h minmax() change/fix [message #25325 is a reply to message #24069] Wed, 17 February 2010 10:24 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
yiy are right, the compiler warns
warning C4172: returning address of local variable or temporary

using a
s = min(max(a, b), c);

//instead of

s = minmax(a, b, c);

does not produce this warning. i still dont get why.

maybe to have a

#define minmax(x, _min, max) min(max(x, _min), _max)

is a choice.

or at least
template <class T>
inline T minmax(const T& x, const T& _min, const T& _max) { return min(max(x, _min), _max); }

reducing the need of object copy by 60% Smile (roughly)

dont know it this is that much of speed saving, but using refs instead of temp copies is better anyway, isnt it?

[Updated on: Wed, 17 February 2010 10:25]

Report message to a moderator

Re: Defs.h minmax() change/fix [message #25373 is a reply to message #25325] Fri, 19 February 2010 16:52 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
kohait00 wrote on Wed, 17 February 2010 04:24

yiy are right, the compiler warns
warning C4172: returning address of local variable or temporary

using a
s = min(max(a, b), c);

//instead of

s = minmax(a, b, c);

does not produce this warning. i still dont get why.

maybe to have a

#define minmax(x, _min, max) min(max(x, _min), _max)

is a choice.

or at least
template <class T>
inline T minmax(const T& x, const T& _min, const T& _max) { return min(max(x, _min), _max); }

reducing the need of object copy by 60% Smile (roughly)

dont know it this is that much of speed saving, but using refs instead of temp copies is better anyway, isnt it?



In this case, I believe there will be no difference in code generated in most cases. It is inline after all. And in fact, it is used on fundamental types 90% of time.

Mirek
Previous Topic: Uuid formating with dashes
Next Topic: ERROR: EXCEPTION_ACCESS_VIOLATION IN CORE ON DEBUGGING!
Goto Forum:
  


Current Time: Mon Apr 29 16:20:43 CEST 2024

Total time taken to generate the page: 0.02948 seconds