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

Discussion in 'C++' started by Nick Keighley, Jun 22, 2006.

  1. 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
     
    Nick Keighley, Jun 22, 2006
    #1
    1. Advertising

  2. Re: strange c++ cast [Was: Named parameters]

    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.std.c /browse_frm/thread/8b7617618a67f610/

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 22, 2006
    #2
    1. Advertising

  3. Re: strange c++ cast [Was: Named parameters]

    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.std.c /browse_frm/thread/8b7617618a67f610/

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 22, 2006
    #3
  4. Re: strange c++ cast [Was: Named parameters]

    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.std.c /browse_frm/thread/8b7617618a67f610/


    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
     
    Nick Keighley, Jun 22, 2006
    #4
  5. 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.
     
    Frederick Gotham, Jun 22, 2006
    #5
  6. Nick Keighley

    Phlip Guest

    [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!!!
     
    Phlip, Jun 22, 2006
    #6
  7. Nick Keighley

    Marcus Kwok Guest

    In comp.lang.c++ Frederick Gotham <> 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
     
    Marcus Kwok, Jun 22, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adam Ruth

    Named parameters

    Adam Ruth, Oct 30, 2003, in forum: C Programming
    Replies:
    48
    Views:
    1,103
    James Hu
    Nov 3, 2003
  2. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,082
    Dan Pop
    Feb 10, 2004
  3. EvilRix
    Replies:
    8
    Views:
    655
    Martin Dickopp
    Feb 14, 2004
  4. Magnus Lyck?
    Replies:
    5
    Views:
    399
    Magnus Lyck?
    Dec 2, 2003
  5. Pavel
    Replies:
    7
    Views:
    540
    Pavel
    Sep 19, 2010
Loading...

Share This Page