Many libraries like boost use ::new
and ::delete
.
许多库如boost use::new和:delete。
Example from boost::make_shared
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?
这是什么意思?为什么要用:new而不是just new?
5 个解决方案
#1
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).
类C可以定义自己的操作符new(例如,它允许为该类拥有自己的分配策略,并/或为该类提供一些分配器。许多标准容器模板接受可选的分配器参数,例如std::vector的第二个参数;参见std::分配器和这个例子)。
If you code new C
, that operator would be used (if it exists).
如果您编写新的C,将使用该操作符(如果存在的话)。
If you code ::new C
, the global new
is used
如果您编码::new C,则使用全局new
Your example is using the global placement new
您的示例使用了全局布局new。
#2
20
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.
当您看到::new时,您正在使用全局新操作符。
#3
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.
通常,当在LHS中使用范围解析(::)操作符而不使用任何说明符时,它指的是全局范围。这里也是一样的。
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.
到操作符new,它可以在本地和外部的范围重载。因此,使用全局变量范围解析操作符。
#4
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
.
这是我们试图在位置pv中构造T的地方。
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.
为了避免调用new的覆盖而不是“真正的”位置新,您必须使用:new(void指针)类型(参数…);这个新特性既不能被替换,也不能被重写。
#5
-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.
正常的新操作符是在类级别实现的,可以是overriden,而:new是在全局级别实现的,不能是overriden。
#1
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).
类C可以定义自己的操作符new(例如,它允许为该类拥有自己的分配策略,并/或为该类提供一些分配器。许多标准容器模板接受可选的分配器参数,例如std::vector的第二个参数;参见std::分配器和这个例子)。
If you code new C
, that operator would be used (if it exists).
如果您编写新的C,将使用该操作符(如果存在的话)。
If you code ::new C
, the global new
is used
如果您编码::new C,则使用全局new
Your example is using the global placement new
您的示例使用了全局布局new。
#2
20
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.
当您看到::new时,您正在使用全局新操作符。
#3
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.
通常,当在LHS中使用范围解析(::)操作符而不使用任何说明符时,它指的是全局范围。这里也是一样的。
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.
到操作符new,它可以在本地和外部的范围重载。因此,使用全局变量范围解析操作符。
#4
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
.
这是我们试图在位置pv中构造T的地方。
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.
为了避免调用new的覆盖而不是“真正的”位置新,您必须使用:new(void指针)类型(参数…);这个新特性既不能被替换,也不能被重写。
#5
-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.
正常的新操作符是在类级别实现的,可以是overriden,而:new是在全局级别实现的,不能是overriden。