相关疑难解决方法(0)

根据模板参数选择联合成员

我正在处理C++中的union,我希望有一个函数模板,它可以根据模板参数访问活动的union成员.

代码就像(doSomething只是一个例子):

union Union {
  int16_t i16;
  int32_t i32;
};

enum class ActiveMember {
    I16 
  , I32
}

template <ActiveMember M>
void doSomething(Union a, const Union b) {
  selectMemeber(a, M) = selectMember(b, M);
  // this would be exactly (not equivalent) the same
  // that a.X = b.X depending on T.
}
Run Code Online (Sandbox Code Playgroud)

为了实现这一点,我只发现了一些糟糕的黑客攻击,比如专业化,或者是一种不同类的访问和分配方式.

我错过了什么,这样的事情应该用其他方法来做?

c++ templates member unions

7
推荐指数
1
解决办法
3784
查看次数

标签 统计

c++ ×1

member ×1

templates ×1

unions ×1