为什么Boost中有两个变体类实现? [英] Why are there two variant class implementations in Boost?
问题描述
Boost似乎具有变体类模板的两种实现:
Boost seems to have two implementations of a variant class template:
Boost很少有(虽然不是闻所未闻)两个包含两个相同概念的东西.为什么变体会发生这种情况?这些变体有何不同?
It is rare (though not unheard of) for Boost two include two takes on the same concept. Why has this happened for variants? How do these variants differ?
推荐答案
第一种变体类模板 boost :: variant
早于C ++ 17的 std :: variant
.有关与 std :: variant
的比较,请参见此问题.主要是,差异涉及在变体中构造一个值时引发异常时的处理方式.
The first kind of variant class template, boost::variant
, predates C++17's std::variant
. See this question for its comparison to std::variant
. Mainly, the difference regards what to do when an exception is thrown on construction of a value within the variant.
std :: variant
选择是允许无值状态; boost :: variant
选择是在堆上而不是就地构造新对象,并存储指向该位置的指针.
The std::variant
choice is to allow a valueless state; the boost::variant
choice is to construct the new object on the heap, not in-place, and store a pointer to that location.
std :: variant2
是后来的版本,它一方面希望遵守C ++ 17 API,另一方面却表达了对其选择的不满在这个问题上.
std::variant2
is a later addition, which, on the one hand, wishes to adhere to the C++17 API, but on the other hand is an expression of dissatisfaction with its choice on this matter.
std :: variant2
选择第三个选项,与之前的两个实现不同:双缓冲.它占据了有问题类型的两倍大小;在未使用的一半中构造一个新值,一旦构造成功-则破坏另一半中的旧值.如果所有类型都不都是可构造的,则这不是必需的,并且 boost :: variant2
实际上不会具有双倍缓冲区.
std::variant2
chooses a third option, different than both previous implementations: double-buffering. It takes up twice the size of the problematic type; constructs a new value in the unused half, and once the construction succeeds - destroys the old value in the other half. If all types are nothrow-constructible, this is not necessary and boost::variant2
will not actually have the double buffer.
此选择意味着 boost :: variant2
不可能毫无价值;实际上,其文档标题强调了这一事实.
This choice means that boost::variant2
can never be valueless; and indeed, its documentation title emphasizes this fact.
这篇关于为什么Boost中有两个变体类实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!