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 

template needs to handle invalid floating point without comp

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





PostPosted: Thu Nov 10, 2005 5:56 pm    Post subject: template needs to handle invalid floating point without comp Reply with quote



Hi!

I need a method of implementing the following concept without
compilation errors:

template <typename C>
C myclass<C>::and(C x, C y)
{
if( not floating point C ) // tested with if(0)
{
return x & y;
}
else
{
return 0;
}
}

Unfortunately, even though I get warned about unreachable code in the
test case, the compiler still rejects a floating point x & y;

Thanks,

Don Ziesig

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

Back to top
Jeffrey Schwab
Guest





PostPosted: Thu Nov 10, 2005 8:24 pm    Post subject: Re: template needs to handle invalid floating point without Reply with quote



Don Ziesig wrote:
Quote:
Hi!

I need a method of implementing the following concept without
compilation errors:

template <typename C
C myclass {
if( not floating point C ) // tested with if(0)
{
return x & y;
}
else
{
return 0;
}
}

Unfortunately, even though I get warned about unreachable code in the
test case, the compiler still rejects a floating point x & y;

Rather than using if/else, just specialize the template for special
cases. E.g:

template< typename C >
C bitwise_and( C x, C y )
{
return x & y;
}

template< >
double bitwise_and( double x, double y )
{
return 0;
}

#include <iostream>

int main( )
{
std::cout << bitwise_and( 3, 5 ) << "n";
std::cout << bitwise_and( 3.0, 5.0 ) << "n";
}

And be careful: "and" is a keyword.

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


Back to top
richard@ex-parrot.com
Guest





PostPosted: Thu Nov 10, 2005 8:24 pm    Post subject: Re: template needs to handle invalid floating point without Reply with quote




Don Ziesig wrote:
Quote:

I need a method of implementing the following concept without
compilation errors:

template <typename C
C myclass

First of all, "and" cannot be used as a function name as it is an
alternative spelling of the token &&.

Quote:
{
if( not floating point C ) // tested with if(0)

Do you mean that you need to test whether C is a valid floating point
type? If so, dispatching via an is_floating_point type traits such as
the one in boost or TR1 will do this:

template <typename C>
C myclass<C>::my_and_2(C x, C y, std::tr1::true_type) {
return 0; // It is a floating point
}

template <typename C>
C myclass<C>::my_and_2(C x, C y, std::tr1::false_type) {
return x & y; // It isn't a floating point
}

template <typename C>
C myclass<C>::my_and(C x, C y) {
return my_and_2(x, y, std::tr1::is_floating_point<C>() );
}

--
Richard Smith


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


Back to top
Dan McLeran
Guest





PostPosted: Thu Nov 10, 2005 8:24 pm    Post subject: Re: template needs to handle invalid floating point without Reply with quote

You can't parametize templates with floats.


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

Back to top
mlimber
Guest





PostPosted: Fri Nov 11, 2005 12:53 am    Post subject: Re: template needs to handle invalid floating point without Reply with quote

Don Ziesig wrote:
Quote:
Hi!

I need a method of implementing the following concept without
compilation errors:

template <typename C
C myclass {
if( not floating point C ) // tested with if(0)
{
return x & y;
}
else
{
return 0;
}
}

Unfortunately, even though I get warned about unreachable code in the
test case, the compiler still rejects a floating point x & y;

Try partial specialization:

template <typename C>
C and ( const C x, const C y )
{
return x & y;
}

template<>
float and<>( const float, const float )
{
return 0;
}

You can add specializations for other types (e.g. double), also, and
Boost and Loki each provide type lists and type traits, which might
make the job simpler for a larger number of specializations.

Cheers! --M


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


Back to top
Dan McLeran
Guest





PostPosted: Fri Nov 11, 2005 9:04 am    Post subject: Re: template needs to handle invalid floating point without Reply with quote

Oops, i misunderstood the question. I thought you were asking if you
could do this:

template<float f>

Sorry.


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

Back to top
Don Ziesig
Guest





PostPosted: Fri Nov 11, 2005 9:07 am    Post subject: Re: template needs to handle invalid floating point without Reply with quote

Jeffrey Schwab wrote:
Quote:
Don Ziesig wrote:
Hi!

I need a method of implementing the following concept without
compilation errors:

template <typename C
C myclass {
if( not floating point C ) // tested with if(0)
{
return x & y;
}
else
{
return 0;
}
}

Unfortunately, even though I get warned about unreachable code in the
test case, the compiler still rejects a floating point x & y;

Rather than using if/else, just specialize the template for special
cases. E.g:

template< typename C
C bitwise_and( C x, C y )
{
return x & y;
}

template
double bitwise_and( double x, double y )
{
return 0;
}

#include
int main( )
{
std::cout << bitwise_and( 3, 5 ) << "n";
std::cout << bitwise_and( 3.0, 5.0 ) << "n";
}

And be careful: "and" is a keyword.

Hello again!

I was using the pseudo code as an example of what I wanted to do [I
really don't have "and" as a function name Wink ].

As it turns out, what I thought might be a quick and easy fix (it looked
like there was a lot of code that would have to be changed if I couldn't
do what I wanted) turned out to be a quick and easier fix when the
suggestion of specialization was made earlier in the thread. I was able
to eliminate over 1000 lines of miserable hacking by re-ordering the
inheritance chain and eliminating the floating point logicals (and other
compilation-error producing code) by specializing logical ops in the
integer type descendants only!

Thanks all!!!!!!

Donz

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


Back to top
wkaras@yahoo.com
Guest





PostPosted: Fri Nov 11, 2005 9:12 am    Post subject: Re: template needs to handle invalid floating point without Reply with quote

Don Ziesig wrote:
Quote:
Hi!

I need a method of implementing the following concept without
compilation errors:

template <typename C
C myclass {
if( not floating point C ) // tested with if(0)
{
return x & y;
}
else
{
return 0;
}
}
....


Here is another approach.

template <typename C, bool Whole>
struct bit_and_
{ C operator () (C x, C y) { return(x & y); } };

template <typename C>
struct bit_and_<C, false>
{ C operator () (C x, C y) { return(0); } };

template <typename C>
C bit_and(C x, C y)
{ bit_and_<C, (((C(1) / 2) * 4) == C(0))> ba;
return(ba(x, y)); }

A simplified alternative:

template <typename C>
C bit_and(C x, C y)
{ return( bit_and_<C, ((C(1) / 2) * 4) == C(0)>()(x, y));

would only compile with a newer version of GCC.


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


Back to top
E. Mark Ping
Guest





PostPosted: Sat Nov 12, 2005 10:31 am    Post subject: Re: template needs to handle invalid floating point without Reply with quote

In article <1131646388.140873.106720 (AT) z14g2000cwz (DOT) googlegroups.com>,
<richard (AT) ex-parrot (DOT) com> wrote:
Quote:
Do you mean that you need to test whether C is a valid floating point
type? If so, dispatching via an is_floating_point type traits such as
the one in boost or TR1 will do this:

template <typename C
C myclass return my_and_2(x, y, std::tr1::is_floating_point<C>() );

Or you can use the existing numeric_limits<C>::is_integer
--
Mark Ping
[email]emarkp (AT) soda (DOT) CSUA.Berkeley.EDU[/email]

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


Back to top
Richard Smith
Guest





PostPosted: Sun Nov 13, 2005 3:35 pm    Post subject: Re: template needs to handle invalid floating point without Reply with quote


E. Mark Ping wrote:
Quote:
template <typename C
C myclass return my_and_2(x, y, std::tr1::is_floating_point<C>() );

Or you can use the existing numeric_limits<C>::is_integer

How will that help? I want to find out whether it's a floating point
number, not whether it's an integer. Plenty of types besides floating
points numbers and integers support operator& -- a std::bitset does,
for example; so do enums (after a fashion and they can be readily made
to work better).

If you don't want to rely on a TR1 component (or, alternatively, a
boost component), it's easy enough to roll your own is_floating_point
class.

--
Richard Smith


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