难道升压:: hold_any构造具有不确定的行为? [英] Does the boost::hold_any constructor have undefined behavior?
问题描述
我在的boost :: hold_any
文件看,我发现了一些东西混淆了我。如果我认为我的信息通过回答我的问题有:<一href=\"http://stackoverflow.com/questions/30051662/what-happens-if-you-call-a-destructor-and-use-the-allocated-memory-again-for-oth\">What如果你调用析构函数,然后再次使用分配的内存为其他对象发生了什么?(在由麦克·西摩的答案),那么它是禁止来操纵它并没有释放,又重新分配对象的内存,放置有一个不同类型的新对象。
我一直以为Boost库棍棒的标准,但如果我不总不会错那么的boost ::内存里面hold_any
商店小物件或变量的这内容指针( void *的对象
)本身,这会打破我在$ p $发现规则pviously提到的答案。
我也是总不会错还是的boost :: hold_any
打破了标准?如果我错了,哪里是我的错?
或者,如果我是对的,多么可靠是如果它们包含的部分,这可能会导致不确定的行为Boost库?
好吧,我知道,提升开发人员通常知道他们在做什么,但是我听到常常不确定的行为有任何情况下都应该避免让我有点好奇。
修改
我改变了code例子来说明,旧的内容definitly被销毁!
这是在点之一的的boost :: hold_any
code,其中我从我的信息:
模板&LT; typename的T&GT;
basic_hold_any&安培;分配(T&const的放大器; X)
{
//我们是同类型之间进行复制?
精神::详细:: fxn_ptr_table&LT;&夏亚GT; * x_table =
精神::详细:: get_table&LT; T&GT; ::模板获得&LT;&夏亚GT;();
如果(表== x_table){
//如果是这样,我们能够避免取消分配和重新使用内存
表 - &GT;自毁(安培;对象); //首先销毁旧的内容
如果(精神::详细:: get_table&LT; T&GT; :: is_small ::值){
//创建-top对象指针本身的副本
新(安培;对象)T(x)的;
}
其他{
//创建顶部的旧版本复制
新的(对象)T(x)的;
}
}
其他{
如果(精神::详细:: get_table&LT; T&GT; :: is_small ::值){
//创建-top对象指针本身的副本
表 - &GT;自毁(安培;对象); //首先销毁旧的内容
新(安培;对象)T(x)的;
}
其他{
重启(); //先删除旧的内容
对象=新的T(x)的;
}
表= x_table; //更新表指针
}
返回*这一点;
}
在链接的答复,UB是基础* PTR
使用,而它仍然指向到存储布局新被称为后不同类型的不同对象被创建那里(它用来调用删除
就可以了)。这不是在升压不限的情况。重用存储不是UB本身。
I looked through the boost::hold_any
file and I found something what confuses me. if I consider the informations I got through the answers to my question: What happens if you call a destructor and use the allocated memory again for other objects? (In the answer by Mike Seymour) then it is "forbidden" to manipulate the memory of an object which weren't released and reallocated yet, to place there a new object of a different type.
I always thought the boost library sticks to the standard, but if I'm not total wrong then boost::hold_any
stores small objects or variables inside the memory of it's content pointer (void* object
) itself, which would break the rule I found in the previously mentioned answer.
So am I total wrong or does boost::hold_any
breaks the standard? And if I'm wrong, where is my mistake?
Or if I'm right, how reliable are the boost libraries if they contain parts, which could cause undefined behavior?
Well, I know that the boost developers normally know what they are doing, but I hear so often undefined behavior has to be avoided under all conditions so I'm a bit curious.
EDIT I changed the code example to show, the old content definitly is destroyed!
This is one of the points in the boost::hold_any
code where I got my information from:
template <typename T>
basic_hold_any& assign(T const& x)
{
// are we copying between the same type?
spirit::detail::fxn_ptr_table<Char>* x_table =
spirit::detail::get_table<T>::template get<Char>();
if (table == x_table) {
// if so, we can avoid deallocating and re-use memory
table->destruct(&object); // first destruct the old content
if (spirit::detail::get_table<T>::is_small::value) {
// create copy on-top of object pointer itself
new (&object) T(x);
}
else {
// create copy on-top of old version
new (object) T(x);
}
}
else {
if (spirit::detail::get_table<T>::is_small::value) {
// create copy on-top of object pointer itself
table->destruct(&object); // first destruct the old content
new (&object) T(x);
}
else {
reset(); // first delete the old content
object = new T(x);
}
table = x_table; // update table pointer
}
return *this;
}
In the linked answer, the UB is that base* ptr
is used while it still points to that storage after placement new was called and a different object of a different type has been created there (it is used to call delete
on it). This is not the case in Boost Any. Reusing the storage is not UB in itself.
这篇关于难道升压:: hold_any构造具有不确定的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!