Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > OT: strange c++ cast [Was: Named parameters]

Reply
Thread Tools

OT: strange c++ cast [Was: Named parameters]

 
 
Nick Keighley
Guest
Posts: n/a
 
      06-22-2006
Frederick Gotham wrote:
> jaysome posted:


[testing an unsigned against -1]

> > Warning 650: Constant '-1' out of range for operator '!='

>
> If I was writing C++ code, I'd have the choice of:
>
> numeric_limits< Type >::max()
>
> But nonetheless I'd still use -1.
>
> As for the warning sending chills up your spine, you can always cast it
> away:
>
> i != (unsigned)-1;
>
> or, in C++:
>
> i != implicit_cast<unsigned>( -1 );


is this a new type of cast?


--
Nick Keighley

"Using a 64-bit value introduces a new wrap around date in about 290
billion
years, on Sunday, December 4, 292,277,026,596 15:30:08 UTC. This
problem is
not, however, widely regarded as a pressing issue."
wiki/Year_2038_problem

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-22-2006
Nick Keighley wrote:
> Frederick Gotham wrote:
>> jaysome posted:

>
> [testing an unsigned against -1]
>
>>> Warning 650: Constant '-1' out of range for operator '!='

>>
>> If I was writing C++ code, I'd have the choice of:
>>
>> numeric_limits< Type >::max()
>>
>> But nonetheless I'd still use -1.
>>
>> As for the warning sending chills up your spine, you can always cast
>> it away:
>>
>> i != (unsigned)-1;
>>
>> or, in C++:
>>
>> i != implicit_cast<unsigned>( -1 );

>
> is this a new type of cast?


I think it exists in Boost.

Also, it's something that was recently under discussion in comp.std.c++.
Check out the thread "Defect: Missing fundamental feature!"

http://groups.google.com/group/comp....617618a67f610/

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-22-2006
Nick Keighley wrote:

[Please don't cross-post C++ questions to comp.lang.c, it gets them mad]

> Frederick Gotham wrote:
>> jaysome posted:

>
> [testing an unsigned against -1]
>
>>> Warning 650: Constant '-1' out of range for operator '!='

>>
>> If I was writing C++ code, I'd have the choice of:
>>
>> numeric_limits< Type >::max()
>>
>> But nonetheless I'd still use -1.
>>
>> As for the warning sending chills up your spine, you can always cast
>> it away:
>>
>> i != (unsigned)-1;
>>
>> or, in C++:
>>
>> i != implicit_cast<unsigned>( -1 );

>
> is this a new type of cast?


I think it exists in Boost.

Also, it's something that was recently under discussion in comp.std.c++.
Check out the thread "Defect: Missing fundamental feature!"

http://groups.google.com/group/comp....617618a67f610/

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      06-22-2006
Victor Bazarov wrote:
> Nick Keighley wrote:
> > Frederick Gotham wrote:
> >> jaysome posted:


> [Please don't cross-post C++ questions to comp.lang.c, it gets them mad]


I wouldn't normally, but the post I was replying to included C++ code
on
comp.lang.c. If it was wrong then I thought it should be pointed out on
clc.
Though of course anyone one who takes C++ advice from comp.lang.c is in

a state of sin...


> > [testing an unsigned against -1]
> >
> >>> Warning 650: Constant '-1' out of range for operator '!='
> >>
> >> If I was writing C++ code, I'd have the choice of:
> >>
> >> numeric_limits< Type >::max()
> >>
> >> But nonetheless I'd still use -1.
> >>
> >> As for the warning sending chills up your spine, you can always cast
> >> it away:
> >>
> >> i != (unsigned)-1;
> >>
> >> or, in C++:
> >>
> >> i != implicit_cast<unsigned>( -1 );

> >
> > is this a new type of cast?

>
> I think it exists in Boost.
>
> Also, it's something that was recently under discussion in comp.std.c++.
> Check out the thread "Defect: Missing fundamental feature!"
>
> http://groups.google.com/group/comp....617618a67f610/


ah. Interesting. Thankyou very much


--
Nick Keighley

"If, indeed the subatomic energy in the stars is being freely
used to maintain their great furnaces, it seems to bring a little
nearer to fulfillment our dreams of controlling this latent
power for the well-being of the human race - or for its suicide."
Aurthur S. Eddington "The Internal Constitution of the Stars" 1926

 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      06-22-2006
Nick Keighley posted:


>> i != implicit_cast<unsigned>( -1 );

>
> is this a new type of cast?



Yes, I realise we're off-topic here, but the three new-style casts are:

static_cast
reinterpret_cast
const_cast


Many people, (myself included), have wanted a fourth one, "implicit_cast".
Until it gets officially added to the language, we can achieve its
functionality using a template provided by Boost.
 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      06-22-2006
[followups set to C++]

Frederick Gotham wrote:

>>> i != implicit_cast<unsigned>( -1 );


> Many people, (myself included), have wanted a fourth one, "implicit_cast".
> Until it gets officially added to the language, we can achieve its
> functionality using a template provided by Boost.


Why should it be a keyword? Or do you just mean someone should move the
template from Boost to the Standard? What would a keyword do that the
template couldn't?

I like languages that permit us to build as many of its keyword-level things
from primitives as possible...

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      06-22-2006
In comp.lang.c++ Frederick Gotham <(E-Mail Removed)> wrote:
> Nick Keighley posted:
>>> i != implicit_cast<unsigned>( -1 );

>>
>> is this a new type of cast?

>
> Yes, I realise we're off-topic here, but the three new-style casts are:
>
> static_cast
> reinterpret_cast
> const_cast


You forgot dynamic_cast.

Followups set to clc++ only.

> Many people, (myself included), have wanted a fourth one, "implicit_cast".
> Until it gets officially added to the language, we can achieve its
> functionality using a template provided by Boost.


--
Marcus Kwok
Replace 'invalid' with 'net' to reply
 
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
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
Strange Behavior for Pages Named Default.aspx Joey ASP .Net 3 06-28-2007 08:28 AM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM



Advertisments