views:

55

answers:

1

I've inherited a Visual Studio 6.0 project to convert to 2005. It includes this fantastic MyClass class below that client code uses everywhere by invoking placement new on an instance of it (greatly simplified here):

#include <new>
#include <cstdio>

template<class T>
class MyClass {
public:

    // This is what the author assumed would be called on placement new.
    inline friend void* operator new(size_t u_size, MyClass<T>& mc) {
        printf("MyClass friend placement new\n");
        // ...
        return 0;
    }

    // This is just to show koenig lookup works on normal functions.
    inline friend void hello(MyClass<T>& mc) {
        printf("Hello called with koenig lookup\n");
        // ...
    }

    // This was part of the original class, gets called further below.
    operator unsigned int*() {
        printf("Converting for default placement new\n");
        // ...
        return 0;
    }
};

/* This gets called in VS2005 if un-commented.
template<class T>
void* operator new(size_t u_size, MyClass<T>& mc) {
    printf("MyClass placement new non-friend non-inline\n");
    // ***
    return 0;
}
*/

class DummyClass {    
   int a;
};

void testfunction() {
    MyClass<DummyClass> mc;
    hello(mc);
    void* a = new(mc) DummyClass; // Placement new call

    char c;
    gets(&c);
}

When I run "testfunction()" in VS2005, at the placement new call, the operator "inline friend void* operator new(...)" in MyClass never gets called. Instead, "operator unsigned int*()" gets called, the result is cast to void*, and the default placement operator new is invoked instead (so "Converting for default placement new" is displayed).

In VS6, the placement new calls "inline friend void* operator new(...)" in MyClass instead (so "CMyClass friend placement new" is displayed), which is what the author intended, but then again VS6 implements inline friends in a weird way.

Why doesn't VS2005 recognize the inline friend placement operator new using argument-dependent lookup? It recognizes the hello() function using arguments (so "Hello called with koenig lookup" is displayed), but it doesn't work for placement new.

For reference, this seems to happen whether or not MyClass is templated or not (but I left it templated for completeness' sake). Also if you un-comment the non-friend "operator new" outside of MyClass, that one gets called properly in VS2005.

What gives? Is there an error in there? Is placement new a special case for argument-dependent lookup? Is VS2005 right or wrong? What would be standard C++ here?

For a workaround I was going to use a non-inline friend instead of inline, but that gets ugly with forwards and all, wanted to ask what the deal is here first.

+3  A: 

The problem is that the allocation function is looked up in the global scope, it is not looked up using ADL. Since friend functions defined inside a class are hidden from the enclosing scope, the function is not found.

5.3.4/9:

If the new-expression begins with a unary :: operator, the allocation function’s name is looked up in the global scope. Otherwise, if the allocated type is a class type T or array thereof, the allocation function’s name is looked up in the scope of T. If this lookup fails to find the name, or if the allocated type is not a class type, the allocation function’s name is looked up in the global scope.

avakar
Ah ok thank you, straightforward enough. So this was just another victim of VC6's promiscuity.
DustOff