Why is overriding both the global new operator and the class-specific operator not ambiguous behaviour?

ostream operator overloading c++ without friend
operator overloading in c++
overloading operator c++
equal operator overloading in c++
friend ostream operator
new operator and operator new
new operator in c++ classes
global operator overloading c++

Consider the following code:

class Foo 
{
public:
    //class-specific
    Foo operator+(Foo& rhs)
    {
       return Foo(); //Just return a temporary
    }

    void* operator new(size_t sd)
    {
        return malloc(sd);
    }
};

//global
Foo operator+(Foo& lhs, Foo& rhs)
{
    return Foo();
}

void* operator new(size_t sd)
{
    return malloc(sd);
}

This code will not compile, stating the call is ambiguous because it matches two operators:

Foo a, b;
a + b;

But this one with the new operator compiles just fine, and will call the class-specific one.

Foo* a = new Foo();

Why doesn't it result in a compile error? Does the compiler treat the new operator differently? (Any citation to the standard would be appreciated.)

Why doesn't it result in a compile error? Does the compiler treat the new operator differently? (Any citation to the standard would be appreciated)

Regarding the precedence between global new and class specific new, the reference says this:

As described in allocation function, the C++ program may provide global and class-specific replacements for these functions. If the new-expression begins with the optional :: operator, as in ::new T or ::new T[n], class-specific replacements will be ignored (the function is looked up in global scope). Otherwise, if T is a class type, lookup begins in the class scope of T.

So class specific new has priority.

Regarding the overload of +, you can either have the member overload or the global overload (usually as a friend of the class) but not both because of the ambiguity it produces.

Why overriding both the global new operator and the class-specific , For member functions of class X , the type of the implicit object parameter is is indistinguishable from other arguments, but the following special rules apply to the an enumeration type, both builtin operators and user-defined operator overloads 2) non-member candidates: For the operators where operator overloading  Your global new operator has no direct relation to the class Foo. A class specific new has precedence over the global new. There is no ambiguity. Your operator+ does specifically relate to the class Foo. There is no precedence between the operator defined outside and the one defined inside the class. Thus you get ambiguity.

The class' operator new is always preferred if defined:

[expr.new]/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.

It can be tricky to read: if the new-expression does not begins with :: and the allocated type is a class type, then new is looked up in the class' scope.

Overload resolution, If you wanted to print an instance of this class to the screen, you'd have to do Overloading operator<< is similar to overloading operator+ (they are both binary single answer by value (because we were creating and returning a new result). This not only prevents a copy of std::ostream from being made, it also allows us​  Why overriding both the global new operator and the class-specific operator is not ambiguous? Operator Overloading in C++; Rules for operator overloading; Types of Operator Overloading in C++; Overloading New and Delete operator in c++; Increment (++) and Decrement (--) operator overloading in C++; C++ program to compare two Strings using

Your global new operator has no direct relation to the class Foo. A class specific new has precedence over the global new. There is no ambiguity.

Your operator+ does specifically relate to the class Foo. There is no precedence between the operator defined outside and the one defined inside the class. Thus you get ambiguity.

9.3, See also specific methods accessor, 275 advantages of modularization, minus sign (-) class diagrams, 327 subtraction operator, 47 modularization, 214. 82–​84 nonstatic methods, 287 NOT operator, 98 Notepad, 21 not-equal-to operator 214–216 ambiguous, avoiding, 242–245 body, 217 calling (invoking), 217 class,​  This code will not compile, stating the call is ambigous because it matches two operators: Foo a, b; a + b; But this one with the new operator compiles just fine, and will call the class-specific one.

An Object-Oriented Approach to Programming Logic and Design, Looking at it this way, you have no reason to overload an operator except if it will shows the syntax to overload all the unary operators, in the form of both global These will expand upon the Integer class shown previously and add a new byte class. This behavior is not restricted to the copy-constructor. Why overriding both the global new operator and the class-specific operator is not ambiguous? Ternary operator vs Null coalescing operator in PHP new vs operator new in C++

12: Operator Overloading, With the end of the Cold War and the general relaxation of military tensions in many areas of the world, global military spending has undergone a significant  new vs operator new in C++ When you create a new object, memory is allocated using operator new function and then the constructor is invoked to initialize the memory. Here, The new operator does both the allocation and the initialization, where as the operator new only does the allocation.

Bulletin of the Atomic Scientists, The equivalent global operator delete is not available. Excepting the specific deallocation function called, we believe that any programs that would new T[5] results in a call of operator new[](sizeof(T)*5+x) , and a usual deallocation function with both a pointer parameter and a size parameter, then the  The overloaded new operator function can accept arguments; therefore, a class can have multiple overloaded new operator functions. This gives the programmer more flexibility in customizing memory allocation for objects. Overloaded new or delete operators also provide Garbage Collection for class’s object.