Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarification on C++ name lookup

Tags:

c++

I have a question about a situation I just came upon at work.

setup: in stringStuff.h

namespace n1
{
namespace n2
{
    typedef std::string myString;
}
}

namespace n1
{
namespace n2
{
    void LTrim(myString& io_string);
    void RTrim(myString& io_string);

    inline void Trim(myString& io_string)
    {
        LTrim(io_string);
        RTrim(io_string);
    }
}
}

in impl.cpp

#include "stringStuff.h" // This actually gets included via other include files
#include "globalInclude.h" // contains 'using namespace n1::n2;'. Yes, I know this isn't best practice

inline static myString Trim(const myString& in_string)
{
    // impl
}

static void impl(const myString& in_string, myString& out_string)
{
    size_t N = 10; // just some computed value.
    out_string = Trim(in_string.substr(1, N)); // This is the line with the error
}

now, I admit I don't understand C++ name resolution rules quite as well as I should, but when I look at this, it seems like the call to Trim should be ambiguous.

The surprising thing is that it compiles just fine on Linux using GCC, calling the function defined in impl.cpp. When I compile on HP-UX using its native compiler, it seems to resolve the call as the one defined in stringStuff.h, and complains about converting a temporary to a non-const ref (which is surprisingly a warning, not an error), and about trying to assign void to a myString.

What should be happening according to the C++ standard, and which compiler (if either) is getting it right?

BTW, I 'fixed' it in my case by prefixing the call to trim with ::, although that isn't really ideal.

like image 292
Bwmat Avatar asked Nov 04 '22 11:11

Bwmat


1 Answers

The trick is that one takes a const reference and the other a non-const reference, so they are two different functions, not the same function whose resolution might be ambiguous. It then comes down to what the type of the parameter in the call is. Since substr returns a value, it can only bind to the const reference. So it should call ::Trim without a warning. I would say that the HP-UX compiler is wrong.

like image 98
Jarryd Avatar answered Nov 12 '22 11:11

Jarryd