AnA*_*ons 3 c++ copy-constructor unions c++11 c++14
就像我有这个结构:
struct S
{
S(const S &arg) : (arg.bIsDouble ? v1{arg.v1} : v{arg.v}) {}
bool bIsDouble{false};
union {
vector<int> v;
double v1;
};
} ;
Run Code Online (Sandbox Code Playgroud)
如何根据某些条件使复制构造函数初始化'v'或'v1'?
我只是将你的工作外包给Boost.Variant:
struct S {
S(const S&) = default;
boost::variant<double, std::vector<int> > v;
};
Run Code Online (Sandbox Code Playgroud)
如果你不想使用Boost,你可以在这里阅读有关如何编写一个有区别的联合,然后实现自己的.
因为你只需要两种类型,这不是太复杂,虽然它仍然非常容易出错并涉及很多代码:
struct DoubleOrVector {
bool is_double;
static constexpr std::size_t alignment_value = std::max(alignof(double), alignof(std::vector));
alignas(alignment_value) char storage[std::max(sizeof(double), sizeof(std::vector))];
DoubleOrVector(double v)
: is_double(true)
{
new (storage) double(v);
}
// similar for vector
DoubleOrVector(const DoubleOrVector& dov)
: is_double(dov.is_double)
{
if (is_double) {
new (storage) double(dov.asDouble());
}
else {
new (storage) std::vector<int>(dov.asVector());
}
}
double& asDouble() {
assert(is_double);
return *reinterpret_cast<double*>(storage);
}
std::vector<int>& asVector() {
assert(!is_double);
return *reinterpret_cast<std::vector<int>*>(storage);
}
// plus other functions here
// remember to explicitly call ~vector() when we need to
};
Run Code Online (Sandbox Code Playgroud)
然后我们仍然默认我们的副本:
struct S {
S(const S&) = default;
DoubleOrVector v;
};
Run Code Online (Sandbox Code Playgroud)