30

Many libraries like boost use ::new and ::delete .

Example from boost::make_shared

template< class T, class... Args > typename boost::detail::sp_if_not_array< T >::type make_shared( Args && ... args )
{
    boost::shared_ptr< T > pt( static_cast< T* >( 0 ), BOOST_SP_MSD( T ) );

    boost::detail::sp_ms_deleter< T > * pd = static_cast<boost::detail::sp_ms_deleter< T > *>( pt._internal_get_untyped_deleter() );

    void * pv = pd->address();

    ::new( pv ) T( boost::detail::sp_forward<Args>( args )... );
    pd->set_initialized();

    T * pt2 = static_cast< T* >( pv );

    boost::detail::sp_enable_shared_from_this( &pt, pt2, pt2 );
    return boost::shared_ptr< T >( pt, pt2 );
}

What does this mean? and why would one use ::new over just new?

timiTao
  • 1,417
  • 3
  • 20
  • 34
tejas
  • 1,795
  • 1
  • 16
  • 34
  • Possible duplicate of [What does ::new mean?](https://stackoverflow.com/questions/14147029/what-does-new-mean) – Scriptable Feb 23 '18 at 11:37

5 Answers5

42

A class C could define its own operator new (this enables, for example, to have your own allocation policy for that class, and/or to provide some Allocator for it. Many standard containers templates accept an optional allocator argument, for example the second argument to std::vector; see also std::allocator and this example).

If you code new C, that operator would be used (if it exists).

If you code ::new C, the global new is used

Your example is using the global placement new

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
21

new, new[], delete, and delete[] (including the placement variants) are overridable both at class and at global scope, although doing the latter is ill-advised.

When you see ::new, you are using the global new operator.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
6

In general whenever scope resolution ( ::)operator is used without any specifier at LHS, it refers to the global scope. Here also it is same.

Coming to the operator new, it can be overloaded at local and outer scope. Hence to access the global variant scope resolution operator is used.

Sitesh
  • 1,816
  • 1
  • 18
  • 25
2

new can be overriden and replaced. So simply stating new won't always get the new you want.

In this specific case, we are looking at placement new:

void * pv = pd->address();

::new( pv ) T( boost::detail::sp_forward<Args>( args )... );

this is where we are trying to construct a T in the location pv.

To avoid the possibility that overrids of new are invoked instead of the "real" placement new, you have to use ::new( void pointer here ) type( arguments... );. That specific new can neither be replaced nor overridden.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • Can it not? Practically speaking it usually can - most c++ runtimes provide a static library and as c++ does not prevent you defining your own global operator new you can effectively replace the built in implementation most of the time. In terms of the c++ spec it probably falls into 'undefined/implementation defined' behaviour. – Chris Becke Feb 23 '18 at 06:30
  • The last paragraph sounds like (the usual `void*`) placement new is a drop-in replacement for plain `new` just to avoid using a different or replaceable `operator new`, but they have rather different behavior: Plain `new` allocates memory for you, but placement new requires some already valid region of memory. – aschepler Feb 23 '18 at 12:54
  • @ChrisBecke The C++ spec does define a number of requirements on any user-defined replacement of global `operator new` to make sure it stays mostly sane. If those are disobeyed, yes, you have undefined behavior. – aschepler Feb 23 '18 at 12:55
-1

The normal new operator is implemented at the class scale and can be overriden while ::new is implemented in a global scale and can't be overriden.