Бих искал да получа тип в променлив шаблон по индекс. Индексът е посочен като аргумент на шаблона. Успях да намеря „хак“, който работи, но вярвам, че не е в духа на програмирането с вариативни шаблони. Освен това използва допълнителна памет.
Ето кода с някои обяснения:
template <typename... InputPortTypes>
class PipelineReceiver
{
protected:
// This tuple is used for storing types only
// Hence, I would like to get rid of it, but I am not sure how.
std::tuple<
std::function<std::unique_ptr<InputPortTypes> (int)>...
> InputPortsTuple;
// This vector is used for storing the actual objects
// This is needed to be able to access/change its elements
// during run time later on.
// The vector is used for storage of function pointers (i.e. of type std::function)
// that represent methods of another object upstream the pipeline.
std::vector<boost::any> InputPortsVector;
public:
PipelineReceiver()
{
// create an empty vector of the required size
InputPortsVector.resize(sizeof...(InputPortTypes));
}
void connectPorts(int InputPortIndex, boost::any c_OutputPort)
{
// connect ports
InputPortsVector[InputPortIndex] = c_OutputPort;
}
// this function needs to be modified to avoid using InputPortsTuple
template<int N>
void getInputPortValue(void)
{
std::cout <<
*boost::any_cast<decltype(std::get<N>(this -> InputPortsTuple))>(
InputPortsVector[N]
)(0) <<
std::endl;
}
};
Бих искал да премахна обекта InputPortsTuple
и да го заменя с някаква форма на рекурсивна процедура за извеждане на типовете в getInputPortValue
.
В идеалния случай бих искал N
да бъде динамичен параметър вместо аргумент на шаблон. Не съм сигурен обаче дали това е възможно.
std::declval<std::tuple<std::function<std::unique_ptr<InputPortTypes> (int)>...>>()
вместоthis->InputPortsTuple
, освен ако не пропускам нещо. (Знам, че това не е това, което наистина преследвате. Това е просто малко по-лоша алтернатива.) - person   schedule 19.04.2015std::get<N>(tuple)
връща препратка към lvalue, която можете да присвоите без никакви проблеми. - person   schedule 19.04.2015