Zhro
Zhro

Reputation: 2614

Why is this function coming into scope?

This error is present when compiling against Visual C++ 2003 through 2013. It is not present when compiling against g++ 4.9.2.

#include <ios>

int main() {
   left(*(new std::string));

   return 0;
}

The problem is that it reports some some error about the arguments. The result I expect is for it to say "identifier not found" because std::left() should be out of scope!

Example of copied inline function:

STD_BEGIN
inline std::ios_base& __CLRCALL_OR_CDECL left2(std::ios_base& _Iosbase) {
   _Iosbase.setf(std::ios_base::left, std::ios_base::adjustfield);
   return (_Iosbase);
}
_STD_END

I examined ios and found that it's an inline function. I copied it out, stuck it into the std namespace using the same macros, and gave it a new name. But the new function is out of scope?

So why is std::left() and std::right() in scope here?

Upvotes: 3

Views: 74

Answers (1)

Mike Seymour
Mike Seymour

Reputation: 254631

Argument dependent lookup will consider function overloads in a namespace, if one or more arguments have a type that's also in that namespace. In this case, the argument type is std::string, so namespace std is considered when looking for overloads, and std::left is found.

By the way, never write *new in real code; it almost invariably causes a memory leak.

Upvotes: 4

Related Questions