Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

interesting question about new and nothrow new

0 views
Skip to first unread message

George

unread,
Jan 28, 2008, 10:46:59 PM1/28/08
to
Hello everyone,


Why "if classes provide their own new but forget to provide nothrow new too,
nothrow new will be hidden and won't even work."

http://www.gotw.ca/publications/mill16.htm

BTW: I understand we should avoid nothrow new, and now I am just interested
to know why if only provide normal new, there will be issues?


thanks in advance,
George

Igor Tandetnik

unread,
Jan 29, 2008, 12:52:19 AM1/29/08
to
"George" <Geo...@discussions.microsoft.com> wrote in message
news:E7CD95D8-58DE-48CB...@microsoft.com

> Why "if classes provide their own new but forget to provide nothrow
> new too, nothrow new will be hidden and won't even work."
>
> http://www.gotw.ca/publications/mill16.htm

I see you chose to omit the beginning of the sentence you are quoting. I
can't help but wonder why. The sentence happens to begin with "As
pointed out in the previous column, ...". I assume you have read the
previous column, and found the explanation there lacking. In what way
was it not measuring up to your standards?
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


George

unread,
Jan 29, 2008, 2:14:01 AM1/29/08
to
Thanks Igor!


I have read through the article, and I think if we only provide one version
of new in current class, compiler will stop in current class searching for
the other 2 forms of overloaded new even if current class does not provide it
(they are defined in base class or global space).

The issue of providing only one form of new is, when other class uses the
other two forms of new, since in current class, it is not implemented, there
will be compile error.

My understanding correct?

BTW: from the article, I am not sure why it is a compile time error, it
should be more like a runtime error?


regards,
George

Igor Tandetnik

unread,
Jan 29, 2008, 8:22:16 AM1/29/08
to
"George" <Geo...@discussions.microsoft.com> wrote in message
news:7F58A861-CE65-44C5...@microsoft.com

> I have read through the article, and I think if we only provide one
> version of new in current class, compiler will stop in current class
> searching for the other 2 forms of overloaded new even if current
> class does not provide it (they are defined in base class or global
> space).

Yes. This is not really different from this situation:

void f(); // global function

class C {
void f(int);
void g() { f(); } // error
};

When resolving f inside g, the compiler looks in enclosing scope, finds
f(int), and doesn't look any further. It then produces an error since
f(int) requires a parameter that wasn't provided. ::f() is never
considered. It is said that C::f(int) hides ::f().

Similarly, if you define a version of operator new inside the class, it
hides other versions of operator new that might be defined at global
scope.

> The issue of providing only one form of new is, when other class uses
> the other two forms of new, since in current class, it is not
> implemented, there will be compile error.

I don't understand what you are trying to say here. I'm confused by your
references to "current class" and "other class". What are these two
different classes you are talking about?

> BTW: from the article, I am not sure why it is a compile time error,
> it should be more like a runtime error?

What makes you think so? Which issues do you feel the compiler cannot
resolve at compile time?

George

unread,
Jan 29, 2008, 8:49:01 PM1/29/08
to
Thanks Igor,


> I don't understand what you are trying to say here. I'm confused by your
> references to "current class" and "other class". What are these two
> different classes you are talking about?

Sorry for my bad English. :-)

My point is, the name is hidden even if the same method has different
parameter, right?


regards,
George

Igor Tandetnik

unread,
Jan 29, 2008, 9:29:54 PM1/29/08
to
"George" <Geo...@discussions.microsoft.com> wrote in message
news:1A6828A0-78C0-4BB8...@microsoft.com

> My point is, the name is hidden even if the same method has different
> parameter, right?

Yes. First, the name lookup happens. This just tries to resolve the
name, and doesn't depend on parameters (except as related to
argument-dependent aka Koenig lookup). The name lookup works from the
current scope up through enclosing scopes. It stops as soon as it finds
the name in some scope, and doesn't consider enclosing scopes from that
point out.

If the name lookup found more than one entity with a given name, then
overload resolution kicks in to choose one candidate. At this point
number and types of parameters of the call are considered.

Access (private, protected, public) is checked last, on the single
candidate picked by overload resolution.

George

unread,
Jan 29, 2008, 10:03:01 PM1/29/08
to
Thanks Igor,


Your reply is great! My question is answered.


regards,
George

0 new messages