Topic: My own operator new


Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Mon, 30 Jul 2001 10:32:52 GMT
Raw View
In article <3b644369.32552147@news.nikoma.de>, Martin Aupperle
<MikeAlpha@NoSpam_csi.com> writes
>Then another question arises. In C/C++ Users Journal March 2001, Herb
>Sutter writes (on p69) about the name lookup in case of operator new:
>
>"In brief, the compiler starts in the current scope; if no instances
>of the name are found, it moves outward to the next enclosing scope
>and repeats. Once it finds a scope containing at least one instance of
>the name, it stops looking and works only with the matches it has
>found ..."
>
>This looks to me like ordinary name lookup for functions. Is Mr.
>Sutter wrong here?

operator new and operator delete also appear to need special handling
else how would you ever select an in class one without ambiguity with
the global ones :)

Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: MikeAlpha@NoSpam_csi.com (Martin Aupperle)
Date: Wed, 1 Aug 2001 17:46:14 GMT
Raw View
On Mon, 30 Jul 2001 10:32:52 GMT, Francis Glassborow
<francis.glassborow@ntlworld.com> wrote:

>In article <3b644369.32552147@news.nikoma.de>, Martin Aupperle
><MikeAlpha@NoSpam_csi.com> writes
>>Then another question arises. In C/C++ Users Journal March 2001, Herb
>>Sutter writes (on p69) about the name lookup in case of operator new:
>>
>>"In brief, the compiler starts in the current scope; if no instances
>>of the name are found, it moves outward to the next enclosing scope
>>and repeats. Once it finds a scope containing at least one instance of
>>the name, it stops looking and works only with the matches it has
>>found ..."
>>
>>This looks to me like ordinary name lookup for functions. Is Mr.
>>Sutter wrong here?
>
>operator new and operator delete also appear to need special handling
>else how would you ever select an in class one without ambiguity with
>the global ones :)
>

In the same way as it is done for other operator functions: if a
member version and a global version are "equally good", the member
version wins.

What do you think of Mr. Sutter's statement? He seems to see things
differently.

Martin
------------------------------------------------
Martin Aupperle
MikeAlpha@NoSpam_csi.com
(remove NoSpam_)
------------------------------------------------

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 1 Aug 2001 18:45:31 GMT
Raw View
In article <3b679b22.2742323@news.nikoma.de>, Martin Aupperle
<MikeAlpha@NoSpam_csi.com> writes
>What do you think of Mr. Sutter's statement? He seems to see things
>differently.

As quoted, (I.e. devoid of context) it only explains part of the
process.


Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: MikeAlpha@NoSpam_csi.com (Martin Aupperle)
Date: Fri, 27 Jul 2001 17:02:15 GMT
Raw View
On Wed, 18 Jul 2001 00:02:09 GMT, Francis Glassborow
<francis.glassborow@ntlworld.com> wrote:

>In article <3b529831.435289983@news.nikoma.de>, Martin Aupperle
><MikeAlpha@NoSpam_csi.com> writes
>>But I was surprised to be able to write
>>
>>       X* x = new X;   // #2
>>
>>Is the compiler wrong here? I thought that overloading is only
>>possible in the same scope. I.E. When I have only one class specific
>>operator new, the compiler MUST use it.
>
>
>The overloading rules for operators have always been broader. I think
>the compiler is correct.
>
>
Can you please give me a hint where you took this information from? I
did not find special rules for overloading of operators (vs.
overloading of functions).

Thanks - Martin

------------------------------------------------
Martin Aupperle
MikeAlpha@NoSpam_csi.com
(remove NoSpam_)
------------------------------------------------

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Sat, 28 Jul 2001 10:08:19 GMT
Raw View
In article <3b610c3b.3053911@news.nikoma.de>, Martin Aupperle
<MikeAlpha@NoSpam_csi.com> writes
>Can you please give me a hint where you took this information from? I
>did not find special rules for overloading of operators (vs.
>overloading of functions).

Consider:

class X {
public:
   X & operator=(X const &);
// rest
};


Without broader rules (well Koenig lookup now extends the rules for
ordinary functions) you would not be able to us an equal sign
(assignment operation) in the definition of operator= because the
compiler would always find it in the scope of the declaration, that
would make it a little bit difficult to overload assignment. The same
problem exists for many operators. That is why the look-up rules were
always multi-scope for operators.


Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Russell Kuyper Jr." <kuyper@wizard.net>
Date: Sat, 28 Jul 2001 15:01:13 GMT
Raw View
Francis Glassborow wrote:
>
> In article <3b610c3b.3053911@news.nikoma.de>, Martin Aupperle
> <MikeAlpha@NoSpam_csi.com> writes
> >Can you please give me a hint where you took this information from? I
> >did not find special rules for overloading of operators (vs.
> >overloading of functions).
>
> Consider:
>
> class X {
> public:
>    X & operator=(X const &);
> // rest
> };
>
> Without broader rules (well Koenig lookup now extends the rules for
> ordinary functions) you would not be able to us an equal sign
> (assignment operation) in the definition of operator= because the
> compiler would always find it in the scope of the declaration, that
> would make it a little bit difficult to overload assignment. The same
> problem exists for many operators. That is why the look-up rules were
> always multi-scope for operators.

You've given the rationale for a rule, but you still haven't identified
the rule. He didn't ask "why", he asked "where". I believe you're
referring to 13.3.1.2. As applied to the case Martin was talking about,
this means that a qualified lookup is done using "T::operator new", but
that this is supplemented by unqualified name lookup for "operator new",
with member functions explicitly excluded from the lookup. It's that
second lookup which finds ::operator new().

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: MikeAlpha@NoSpam_csi.com (Martin Aupperle)
Date: Sun, 29 Jul 2001 22:35:53 GMT
Raw View
On Sat, 28 Jul 2001 10:08:19 GMT, Francis Glassborow
<francis.glassborow@ntlworld.com> wrote:

>In article <3b610c3b.3053911@news.nikoma.de>, Martin Aupperle
><MikeAlpha@NoSpam_csi.com> writes
>>Can you please give me a hint where you took this information from? I
>>did not find special rules for overloading of operators (vs.
>>overloading of functions).
>
>Consider:
>
>class X {
>public:
>   X & operator=(X const &);
>// rest
>};
>
>
>Without broader rules (well Koenig lookup now extends the rules for
>ordinary functions) you would not be able to us an equal sign
>(assignment operation) in the definition of operator= because the
>compiler would always find it in the scope of the declaration, that
>would make it a little bit difficult to overload assignment.

Good point.  When I understand you right, an implementation like this
one

X& X::operator = ( X const & arg )
{
  int i;
  i = 3; //???
}

would not be possible because of the assignment of 3 to i.

Then another question arises. In C/C++ Users Journal March 2001, Herb
Sutter writes (on p69) about the name lookup in case of operator new:

"In brief, the compiler starts in the current scope; if no instances
of the name are found, it moves outward to the next enclosing scope
and repeats. Once it finds a scope containing at least one instance of
the name, it stops looking and works only with the matches it has
found ..."

This looks to me like ordinary name lookup for functions. Is Mr.
Sutter wrong here?

Martin




------------------------------------------------
Martin Aupperle
MikeAlpha@NoSpam_csi.com
(remove NoSpam_)
------------------------------------------------

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: MikeAlpha@NoSpam_csi.com (Martin Aupperle)
Date: Mon, 30 Jul 2001 03:18:42 GMT
Raw View
On Sat, 28 Jul 2001 15:01:13 GMT, "James Russell Kuyper Jr."
<kuyper@wizard.net> wrote:

>
>You've given the rationale for a rule, but you still haven't identified
>the rule. He didn't ask "why", he asked "where". I believe you're
>referring to 13.3.1.2.

That's the place I was looking for - thanks. I must have overlooked it
during my search.

>As applied to the case Martin was talking about,
>this means that a qualified lookup is done using "T::operator new", but
>that this is supplemented by unqualified name lookup for "operator new",
>with member functions explicitly excluded from the lookup. It's that
>second lookup which finds ::operator new().

Obviously, yes. But see my other post in this thread about Herb
Sutter's article in March 2001 CUJ.

Martin

------------------------------------------------
Martin Aupperle
MikeAlpha@NoSpam_csi.com
(remove NoSpam_)
------------------------------------------------

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: MikeAlpha@NoSpam_csi.com (Martin Aupperle)
Date: Tue, 17 Jul 2001 22:41:20 GMT
Raw View
Hello,

I have a class

 struct  X
 {
   void* operator new ( size_t, int, int );
   ...
 };

and I of course can write

 X* x = new( 1, 2 ) X;   // #1

But I was surprised to be able to write

 X* x = new X;   // #2

Is the compiler wrong here? I thought that overloading is only
possible in the same scope. I.E. When I have only one class specific
operator new, the compiler MUST use it.

Martin


------------------------------------------------
Martin Aupperle
MikeAlpha@NoSpam_csi.com
(remove NoSpam_)
------------------------------------------------

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 18 Jul 2001 00:02:09 GMT
Raw View
In article <3b529831.435289983@news.nikoma.de>, Martin Aupperle
<MikeAlpha@NoSpam_csi.com> writes
>But I was surprised to be able to write
>
>       X* x = new X;   // #2
>
>Is the compiler wrong here? I thought that overloading is only
>possible in the same scope. I.E. When I have only one class specific
>operator new, the compiler MUST use it.


The overloading rules for operators have always been broader. I think
the compiler is correct.


Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]