Return what?



  • string GetManagerId() { return false; }

    Found yesterday. I can't even figure out how it compiles.



  • the return value is being automatically converted to "False" or to "Boolean" is my guess.  This is probably old code thats no longer in use so someone removed the actual code from the method.



  • Assuming that this is C++, I can see the compiler making a couple of implicit typecasts here (even though it shouldn't).

    For many C++ implementations, false == 0 and NULL == 0. Therefore, false == NULL.

    std::string has a constructor that takes char*, and NULL is a valid pointer value, so the compiler is quite happily compiling the equivalent of std::string(NULL).

    I'd hate to see what happened at run-time though...



  • @eimaj2nz said:

    std::string has a constructor that takes char*, and NULL is a valid pointer value, so the compiler is quite happily compiling the equivalent of std::string(NULL).

    I'd hate to see what happened at run-time though...

     

    To oblige:

    heron@heron6400 /tmp $ cat main.cpp
    #include <iostream>
    #include <string>

    using namespace std;

    string FooBar() { return false; }

    int main()
    {
        cout << "Hello, " << FooBar() << "!" << endl;

        return 0;
    }

    heron@heron6400 /tmp $ g++ -Wall -Wextra main.cpp

    heron@heron6400 /tmp $ ./a.out
    terminate called after throwing an instance of 'std::logic_error'
      what():  basic_string::_S_construct NULL not valid
    Aborted

    heron@heron6400 /tmp $

     

    Not even a warning at compile-time, but it barfs at runtime.



  • @Heron said:

    Not even a warning at compile-time
    <font size="5">!</font>



  • Quoete: For many C++ implementations, false == 0 and NULL == 0. Therefore, false == NULL.

    Hopefully all implementations. Because the c++ standard say that false evaluate to the integer value 0. (And true evaluate to 1. Always).

    And any constant expression(Such as false) that evaluate to 0 is a null pointer. (Who got that idear??? Even Bjarne thought this was a bad idear first time he saw it). 



  • @Heron said:

    Not even a warning at compile-time, but it barfs at runtime.

    Well, that's the STL for you. Great piece of slowdownware... 



  • @TGV said:

    Well, that's the STL for you. Great piece of slowdownware... 

    The STL classes are actually pretty fast (in terms of runtime and development time), unless you're using them wrong... but in any case, your comment is irrelevant to the code sample I provided (which could easily be duplicated without using any portion of the STL).



  • @Zecc said:

    @Heron said:

    Not even a warning at compile-time
    <font size="5">!</font>

    Yes, you're right, that's damned silly and we can do better than that; I don't see why we shouldn't warn about this when we warn about printf-format args.

    Here, try this:

    [quote user="http://pastebin.com/f55a82530"]

     --- basic_string.h.orig    2009-04-18 19:39:38.718750000 +0100
    +++ basic_string.h    2009-04-18 19:46:20.250000000 +0100
    @@ -455,13 +455,13 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
            *  meaning.
            */
           basic_string(const _CharT* __s, size_type __n,
    -           const _Alloc& __a = _Alloc());
    +           const _Alloc& __a = _Alloc()) __attribute__((nonnull (1)));
           /**
            *  @brief  Construct string as copy of a C string.
            *  @param  s  Source C string.
            *  @param  a  Allocator to use (default is default allocator).
            */
    -      basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
    +      basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()) __attribute__((nonnull (1)));
           /**
            *  @brief  Construct string as multiple characters.
            *  @param  n  Number of characters.

    [/quote] 

    You can apply it locally to your installed copy, which will probably be in one of

    /usr/include/c++/${gxx-version}/bits/

    /usr/lib/gcc/${gxx-target}/${gxx-version}/include/c++/bits/

    With that change, I now get:

     

    $ cat main.cpp
    #include <iostream>
    #include <string>

    using namespace std;

    string FooBar() { return false; }

    int main()
    {
        cout << "Hello, " << FooBar() << "!" << endl;
        printf ("Hello %s!\n", 0);
        return 0;
    }

    $ g++ -Wall -Wextra main.cpp
    main.cpp: In function 'std::string FooBar()':
    main.cpp:6: warning: null argument where non-null required (argument 1)
    main.cpp: In function 'int main()':
    main.cpp:11: warning: format '%s' expects type 'char*', but argument 2 has type 'int'
    main.cpp:11: warning: format '%s' expects type 'char*', but argument 2 has type 'int'

    I'll submit it upstream :)

    Hmm, I added the printf just for example.  Seems GCC liked it so much, it warned about it twice.  Wonder what that's all about.


Log in to reply