C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C style casting or c++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
phal
Guest





PostPosted: Fri Dec 16, 2005 10:38 am    Post subject: C style casting or c++ Reply with quote



Hi All,

-------------
#include <iostream>

using namespace std;

template<class A, class B, class C>
inline A max(B const& num1, C const& num2)
{
return (A)num1>(A)num2?(A)num1:(A)num2;
}

How can i implement this template function more c++ style?
Any improvement are welcome?

phal


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Heinz Ozwirk
Guest





PostPosted: Fri Dec 16, 2005 1:40 pm    Post subject: Re: C style casting or c++ Reply with quote



"phal" <betterdie (AT) gmail (DOT) com> schrieb im Newsbeitrag
news:1134714493.932934.186340 (AT) o13g2000cwo (DOT) googlegroups.com...
Quote:
Hi All,

-------------
#include <iostream

using namespace std;

template inline A max(B const& num1, C const& num2)
{
return (A)num1>(A)num2?(A)num1:(A)num2;
}

How can i implement this template function more c++ style?
Any improvement are welcome?

Avoid C-style casts (and all other casts). If you really need to cast, use
static_cast or dynamic_cast. It they don't work, sit back and think about
what you are doing.

Your max template is not better than a macro. Actually, due to the casts, it
is even worse than most min/max macros I have seen. If you really need a max
function, that takes different types of argument and even returns a third
type, don't hide those dangerous casts where the user of that function
cannot see them. What will a user of your template function think, when he
calls

max<unsigned short>(0, -1)

? And what do you think, the result will be?

Heinz



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Michiel.Salters@tomtom.co
Guest





PostPosted: Fri Dec 16, 2005 1:41 pm    Post subject: Re: C style casting or c++ Reply with quote




phal wrote:
Quote:
Hi All,

-------------
#include <iostream

using namespace std;

template inline A max(B const& num1, C const& num2)
{
return (A)num1>(A)num2?(A)num1:(A)num2;
}

How can i implement this template function more c++ style?

template<class A, class B, class C>
inline A max(B const& num1, C const& num2)
{
return std::max<A>(num1, num2);
}
which begs the question why you din't use std::max in the first place.

HTH,
Michiel Salters


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Nick C
Guest





PostPosted: Fri Dec 16, 2005 1:42 pm    Post subject: Re: C style casting or c++ Reply with quote


"phal" <betterdie (AT) gmail (DOT) com> wrote

Quote:
Hi All,

-------------
#include <iostream

using namespace std;

template inline A max(B const& num1, C const& num2)
{
return (A)num1>(A)num2?(A)num1:(A)num2;
}

How can i implement this template function more c++ style?
Any improvement are welcome?

phal

Hi Phal,

Why would you want to deduce the greater of potentially unrelated classes in
this way? If they are related, then can tey have a common base class? This
way, you would not need tamplates at all?

But then, maybe I do not have good understanding of the issues.

Cheers

Nick



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Markus Moll
Guest





PostPosted: Fri Dec 16, 2005 1:50 pm    Post subject: Re: C style casting or c++ Reply with quote

phal wrote:

Hi

Quote:
template<class A, class B, class C
inline A max(B const& num1, C const& num2)
{
return (A)num1>(A)num2?(A)num1:(A)num2;
}

How can i implement this template function more c++ style?
Any improvement are welcome?

I'd rather make the cast explicit where it is needed and use std::max.
Casting inside your max function is imo error-prone.
So instead of writing:

float x = 0.5;
int y = 42;
double z = max<double>(x,y);

I'd write:

float x = 0.5;
int y = 42;
double z = std::max( double(x), double(y) );

Or maybe have a static_cast... depends...

Markus


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ulrich Eckhardt
Guest





PostPosted: Sat Dec 17, 2005 10:48 am    Post subject: Re: C style casting or c++ Reply with quote

phal wrote:
Quote:
#include <iostream

using namespace std;

Bad idea, 'std' already contains a symbol called max. Also, I don't think
it's necessary for the example.

Quote:
template inline A max(B const& num1, C const& num2)
{
return (A)num1>(A)num2?(A)num1:(A)num2;
}

How can i implement this template function more c++ style?
Any improvement are welcome?

Several possibilities:
0. You could use std::max.
1. The casts should be made 'static_cast<A>(..)'. This prevents conversion
between unrelated pointer types and discarding cv qualifiers.
2. You could use function syntax, i.e. "A(x)" instead of "(A)x".
3. You could convert just the result, i.e.
static_cast<A>(num1>num2?num1:num2). This however might introduce a change
in behaviour when ((A)num1>(A)num2) != (num1>num2).

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Alf P. Steinbach
Guest





PostPosted: Sun Dec 18, 2005 12:43 pm    Post subject: Re: C style casting or c++ Reply with quote

* phal:
Quote:

#include <iostream

No, you don't need iostreams.


Quote:
using namespace std;

No, you absolutely don't need that, and namespace std already has a 'max'.


Quote:
template inline A max(B const& num1, C const& num2)
{
return (A)num1>(A)num2?(A)num1:(A)num2;
}

How can i implement this template function more c++ style?

<url: http://www.cuj.com/documents/s=7996/cujcexp1904alexandr/alexandr.htm>

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Gerhard Menzl
Guest





PostPosted: Mon Dec 19, 2005 11:01 am    Post subject: Re: C style casting or c++ Reply with quote

Nick C wrote:

Quote:
template<class A, class B, class C
inline A max(B const& num1, C const& num2)
{
return (A)num1>(A)num2?(A)num1:(A)num2;
}

Why would you want to deduce the greater of potentially unrelated
classes in this way? If they are related, then can tey have a common
base class? This way, you would not need tamplates at all?

A, B, and C are not necessarily classes. They could (and with a function
template named "max" often will) be built-ins, such as char, int, or double.


--
Gerhard Menzl

#dogma int main ()

Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.