我有这样的代码:
struct A1{
void do_something1(int t, ... ){
switch(t){
default:
case 8: return process_<uint8_t >(a, b, c);
case 16: return process_<uint16_t>(a, b, c);
case 32: return process_<uint32_t>(a, b, c);
case 64: return process_<uint64_t>(a, b, c);
}
}
template<typename T>
void process_(...){
}
};
struct A2{
void do_something2(int t, ... ){
switch(t){
default:
case 8: return process_<uint8_t >(d, e, f);
case 16: return process_<uint16_t>(d, e, f);
case 32: return process_<uint32_t>(d, e, f);
case 64: return process_<uint64_t>(d, e, f);
}
}
template<typename T>
void process_(...){
}
};
具有类似开关的几个类将相同的参数“转发”到另一个具有模板参数而不是方法参数t
的方法。
目前,如果我需要更改process_
的参数,我需要将它们写入4次。
是否有简单的方法可以删除此重复项?
例如,代替4个几乎相同的行,仅具有1-2行代码。
switch(t){
default:
case 8: return process_<uint8_t >(d, e, f);
case 16: return process_<uint16_t>(d, e, f);
case 32: return process_<uint32_t>(d, e, f);
case 64: return process_<uint64_t>(d, e, f);
}
我可以考虑方法指针,但我不确定如何做。
2条答案
按热度按时间8e2ybdfx1#
你可以创建一个helper函数,将运行时值
t
“提升”为四种类型之一,并将其传递给一个lambda:bsxbgnwa2#
我可以考虑方法指针,但我不确定如何做。
类似这样的方法应该可以奏效: