Pointer or false?



  • Stumbled across this while trying to help one of our 'Content Developers' write a LUA script for error checking their content files. Specifically, they needed to walk a list of waypoints and check those waypoints to see if any of them had a particular action modifier attached to it. Not horribly complicated at all, honestly, but there was a slightly odd method for checking the attached objects. I had expected a list iterator, instead I got a list accessor that looked something like this:



    Action* Path::GetAction(unsigned int index) const

    {

        return (m_Actions.size() > index) ? m_Actions[index] : false;

    }



    There was, of course, no method for returning the size of the list so the only way to walk it was to keep looping until it didn't return a valid item. After a minor case of faceplant, I fixed the function and added LUA binding to just get the darned list iterator, which makes life so much easier. Sadly, it seems this code has been in there for quite a while. I'm not sure how it made it through the review process, or how we missed it for this long. I was actually a little surprised we weren't getting a compiler warning for it, but after thinking about it, it makes sense.


    Part of me hopes that one day I'll have a snoofle-level WTF to post up for you to enjoy, but then again, that also somewhat frightens me (probably because it'll be my code that gets posted).



  • @CodeNinja said:

    ... write a LUA script...

    Are you building a powerful cyberweapon?



  • @Speakerphone Dude said:

    @CodeNinja said:
    ... write a LUA script...

    Are you building a powerful cyberweapon?



    God I hope not, it'd probably backfire.



  •  Isn't NULL defined as 0 in C++? Isn't false also defined as 0 when cast to an integer (or pointer)?

     

    Not saying it isn't retarded tho.



  • @pure said:

     Isn't NULL defined as 0 in C++?

    @C++ Draft 2005-10-19 said:
    4.10
    Pointer conversions
    [conv.ptr]

    1
    A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero. A null
    pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable
    from every other value of pointer to object or pointer to function type. Two null pointer values of the same type shall
    compare equal. The conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not
    the sequence of a pointer conversion followed by a qualification conversion (4.4). [my emphasis]
    @pure said:
    Isn't false also defined as 0 when cast to an integer (or pointer)?

    @Same draft said:
    4.5
    Integral promotions
    [conv.prom]



    [...]4
    An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one.



  •  So... yes? 😉



  • @pure said:

     So... yes? 😉

    'Almost,' from the stuff I've quoted - just one bit I forgot which actually makes it 'yes':
    @Draft said:
    18.1
    Types
    [lib.support.types]



    Common definitions.


    [...]

    3
    The macro NULL is an implementation-defined C++ null pointer constant in this International Standard (4.10).188)



    [188]Possible definitions include 0 and 0L, but not (void*)0.



  • @CodeNinja said:

    @Speakerphone Dude said:
    @CodeNinja said:
    ... write a LUA script...

    Are you building a powerful cyberweapon?



    God I hope not, it'd probably backfire.
     

    Don't tell FoxNews


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.