0
对于不具名的标题,我真的不知道该怎么称呼我所问的内容。用派生类型重载函数的替代方法
我想实现以下目标:拥有一个基类类型的容器,其中包含派生类型的实例,访问容器并根据所访问的派生对象的类型调用函数重载。在之前的一个问题中,我询问here我了解到我迄今为止所考虑的静态设计不起作用。我试过的方法是这样的:
struct Int2TypeBase{
};
template <int v>
struct Int2Type : public Int2TypeBase
{
enum
{
value = v
};
};
void f(const Int2Type<0>&){
std::cout << "f(const Int2Type<0>&)" << "\n";
}
void f(const Int2Type<1>&){
std::cout << "f(const Int2Type<1>&)" << "\n";
}
int main(){
using namespace std;
std::vector<std::reference_wrapper<Int2TypeBase>> v;
Int2Type<0> i2t_1;
v.emplace_back(i2t_1);
Int2Type<1> i2t_2;
v.emplace_back(i2t_2);
auto x0 = v[0];
auto x1 = v[1];
f(x0.get()); // After my imagination this would have called void f(const Int2Type<0>&)
f(x1.get()); // After my imagination this would have called void f(const Int2Type<1>&)
}
好了,我要选择的f
正确的过载,然而,这并不编译在编译时间,它是未知哪种类型x0
和x1
实际上有。但是有没有一些可以实现这种行为的替代设计?
不幸的是不适用于我的情况,因为实际上'f'函数有不同的返回类型。 –
@lotolmencre:遗憾的是,您的问题是将这项新要求保密...... –
是的,当我构建最小工作示例时,我没有想到它。 –