我将这样的函数调用的返回值存储在 std::vector 中:
这是一个非常粗略的估计
std::vector<boost::any> pressures;
Printer printerObs1;
Printer printerObs2;
const int initialPressure = 1;
auto pressure = MakeSubject<Pressure>(
BindObservers(printerObs1, printerObs2), initialPressure);//Return type Pressure<Printer, Printer> if I had passed three observers, the return type would have been Pressure<Printer, Printer, Printer> and so on
pressures.push_back(pressure); // This forgets the type.
如果压力还在,我可以很容易地说:
decltype(pressure) *p = boost::any_cast<decltype(pressure)>(&pressures[0]);
p->Change(1999); //class Pressure has a function called Change.
问题是,我如何记住存储在
std::vector<boost::any>
? AFAIK,decltype 甚至不返回可以存储的值?
所以我需要一个可以存储 boost::any __and__ 的容器,记住它是 decltype以便我可以说(这不起作用,但这是我想要实现的本质)
decltype(pressures[0]) *p = boost::any_cast<decltype(pressures[0])>(&pressures[0]);