Topic: new(nothrow) or new(std::nothrow)


Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/06/22
Raw View
"Andrew F. Vesper" <avesper@wn.net> writes:

>Steve Clamage wrote:

>> Other methods are possible. For example, you could do this:
>>
>> #include <new>
>> std::nothrow we_dont_need_no_stinking_exceptions;
>> ...
>> T* p = new (we_dont_need_no_stinking_exceptions) T;

>Shouldn't the line following #include <new> read:

>std::nothrow_t we_dont_need_no_stinking_exceptions;

Yes.

--
Steve Clamage, stephen.clamage@sun.com
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: David R Tribble <dtribble@technologist.com>
Date: 1999/06/23
Raw View
"Andrew F. Vesper" wrote:
>
> Steve Clamage wrote:
>
> > Other methods are possible. For example, you could do this:
> >
> > #include <new>
> > std::nothrow we_dont_need_no_stinking_exceptions;
> > ...
> > T* p = new (we_dont_need_no_stinking_exceptions) T;
>
> Shouldn't the line following #include <new> read:
>
> std::nothrow_t we_dont_need_no_stinking_exceptions;

Or perhaps:

    #include <new>
    static std::nothrow_t  nothrow;

    T * p = new(nothrow) T;

-- David R. Tribble, dtribble@technologist.com --
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: David R Tribble <dtribble@technologist.com>
Date: 1999/06/19
Raw View
Steve Clamage wrote:
>
> David R Tribble <dtribble@technologist.com> writes:
>>
>> I understand that 3.4.2#2 defines the rules for function lookup
>> based on the types of the arguments (and their associated classes
>> and namespaces) given in a function call (5.2.2), but where is this
>> stated for operators? Specifically, where is it stated that we can
>> abbreviate 'new(std::nothrow)' as 'new(nothrow)', since 'new' is an
>> operator and not a function name (as shown in the example in
>> 18.4.1.1)?
>
> It is not allowed.  Operators new and delete are not in namespace
> std, so Koenig lookup will not find "nothrow".
>
> The example you mention (which is not normative) is at best
> incomplete.  There is no definition of type T, and the inclusion
> of standard header <new> is not shown. Add a using-declaration
> for std::nothrow and the code becomes valid.

So if I understand it correctly, in order to use 'new(nothrow)',
all of our code must contain a prior 'using std::nothrow'?

-- David R. Tribble, dtribble@technologist.com --
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/06/19
Raw View
David R Tribble <dtribble@technologist.com> writes:

>So if I understand it correctly, in order to use 'new(nothrow)',
>all of our code must contain a prior 'using std::nothrow'?

The "nothrow" is not a language construct. The non-throwing
version of operator new is a placement-new. It takes one argument
of type nothrow_t. Type nothrow_t and "nothrow", an object of
that type, are declared in namespace std, not in the global
namespace.

Two convenient ways to get a non-throwing new-expression are these:

#include <new>
...
T* p = new (std::nothrow) T;


#include <new>
using std::nothrow;
...
T* p = new (nothrow) T;


Other methods are possible. For example, you could do this:

#include <new>
std::nothrow we_dont_need_no_stinking_exceptions;
...
T* p = new (we_dont_need_no_stinking_exceptions) T;

--
Steve Clamage, stephen.clamage@sun.com


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Andrew F. Vesper" <avesper@wn.net>
Date: 1999/06/20
Raw View
Steve Clamage wrote:

> Other methods are possible. For example, you could do this:
>
> #include <new>
> std::nothrow we_dont_need_no_stinking_exceptions;
> ...
> T* p = new (we_dont_need_no_stinking_exceptions) T;

Shouldn't the line following #include <new> read:

std::nothrow_t we_dont_need_no_stinking_exceptions;

???
--
Andy V (OpenGL Alpha Geek)
"In order to make progress, one must leave the door to the unknown ajar."
Richard P. Feynman, quoted by Jagdish Mehra in _The Beat of a Different Drum_.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/06/17
Raw View
David R Tribble <dtribble@technologist.com> writes:

>I understand that 3.4.2#2 defines the rules for function lookup
>based on the types of the arguments (and their associated classes
>and namespaces) given in a function call (5.2.2), but where is this
>stated for operators? Specifically, where is it stated that we can
>abbreviate 'new(std::nothrow)' as 'new(nothrow)', since 'new' is an
>operator and not a function name (as shown in the example in
>18.4.1.1)?

It is not allowed.  Operators new and delete are not in namespace
std, so Koenig lookup will not find "nothrow".

The example you mention (which is not normative) is at best
incomplete.  There is no definition of type T, and the inclusion
of standard header <new> is not shown. Add a using-declaration
for std::nothrow and the code becomes valid.

--
Steve Clamage, stephen.clamage@sun.com
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: David R Tribble <dtribble@technologist.com>
Date: 1999/06/16
Raw View
[reposted, since the first post didn't seem to take. -drt]

I understand that 3.4.2#2 defines the rules for function lookup
based on the types of the arguments (and their associated classes
and namespaces) given in a function call (5.2.2), but where is this
stated for operators?  Specifically, where is it stated that we can
abbreviate 'new(std::nothrow)' as 'new(nothrow)' (as shown in the
example in 18.4.1.1), since 'new' implies an operator and not a
function name?

-- David R. Tribble, dtribble@technologist.com --
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: David R Tribble <dtribble@technologist.com>
Date: 1999/06/12
Raw View
I understand that 3.4.2#2 defines the rules for function lookup
based on the types of the arguments (and their associated classes
and namespaces) given in a function call (5.2.2), but where is this
stated for operators?  Specifically, where is it stated that we can
abbreviate 'new(std::nothrow)' as 'new(nothrow)', since 'new' is an
operator and not a function name (as shown in the example in
18.4.1.1)?

-- David R. Tribble, dtribble@technologist.com --
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]