Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用可变模板创建模板类元组_C++_C++11_Templates_Variadic Templates - Fatal编程技术网

C++ 使用可变模板创建模板类元组

C++ 使用可变模板创建模板类元组,c++,c++11,templates,variadic-templates,C++,C++11,Templates,Variadic Templates,我正在尝试创建一个可变模板类邮局 template <class... AllInputTypes> class PostOffice { public: PostOffice(AllInputTypes&... akkUboytTypes) : allInputMsgStorage(allInputTypes...) {} ... protected: std::tuple<StorageSlot<AllInputTypes>...>

我正在尝试创建一个可变模板类
邮局

template <class... AllInputTypes>
class PostOffice {
public:
   PostOffice(AllInputTypes&... akkUboytTypes)
   : allInputMsgStorage(allInputTypes...) {}
...
protected:
   std::tuple<StorageSlot<AllInputTypes>...> allInputMsgStorage; //TODO: Will this work?
}
所以在元组中,我试图初始化一个

StorageSlot<AllInputType1>, StorageSlot<AllInputType2>,...
这使得邮局的模板界面有点难看


那么,这是否可行?如果不行,我如何才能让它起作用?

总体概念是正确的-稍加修饰,就可以为构造函数提供完美的转发,以提高效率

此外,StorageSlot中不需要虚拟析构函数-除非要使用动态多态性,否则始终使用零规则:

可编译代码:

#include <tuple>
#include <string>

struct Timer
{};

template<class InputMsgType>
class StorageSlot {
public:

    template<class ArgType>
    StorageSlot(ArgType&& input)
        : readFlag(false),
        writeFlag(false),
        storageField(std::forward<ArgType>(input))
    //TODO initialize timer with period, get period from CAN
    {}

    InputMsgType storageField; //the field that it is being stored into
    bool readFlag; //the flag that checks if it is being read into
    bool writeFlag; //flag that checks if it is being written into
    Timer StorageSlotTimer; //the timer that checks the read and write flag
};


template <class... AllInputTypes>
class PostOffice {
public:

    //
    // note - perfect forwarding
    //
    template<class...Args>
    PostOffice(Args&&... allInputTypes)
    : allInputMsgStorage(std::forward<Args>(allInputTypes)...)
    {

    }

protected:
    std::tuple<StorageSlot<AllInputTypes>...> allInputMsgStorage; //TODO: Will this work? - yes
};





int main()
{
    PostOffice<int, double, std::string> po(10, 20.1, "foo");

}
#包括
#包括
结构计时器
{};
模板
类存储槽{
公众:
模板
StorageSlot(ArgType&&input)
:readFlag(false),
writeFlag(false),
存储字段(标准::转发(输入))
//TODO用周期初始化计时器,从CAN获取周期
{}
InputMsgType storageField;//要将其存储到的字段
bool readFlag;//检查是否正在读取的标志
bool writeFlag;//检查是否正在写入的标志
Timer-StorageSlotTimer;//检查读写标志的计时器
};
模板
班级邮局{
公众:
//
//注意-完美转发
//
模板
邮局(参数和…所有输入类型)
:allInputMsgStorage(标准::转发(allInputTypes)…)
{
}
受保护的:
std::tuple allInputMsgStorage;//TODO:这行吗?-行
};
int main()
{
邮政署采购订单(10,20.1,“foo”);
}

您在编译或运行程序时遇到过任何问题吗?看起来这应该可以正常工作。
StorageSlot
当然是一个模板<代码>存储插槽
可以很好地扩展。它可以编译,但我还没有找到一种方法来测试它是否提供了正确的行为。但我最好奇的是编译器如何解释“std::tuple allInputMsgStorage;”。编译器如何确定“…”指的是可变的“AllInputTypes”,并且它应该为每个StorageSlot元组成员提供可变模板的1个元素?是否有关于编译器如何处理此问题的文献?选择可变模板的第一个元素(因为StorageSlot只需要一个),然后将可变模板的其余部分传递给元组的其余部分或类似的内容?
std::tuple<StorageSlot<AllInputTypes...>> allInputMsgStorage;
std::tuple<StorageSlot<AllInputTypes>...> allInputMsgStorage;
std::tuple<AllInputTypeStorageSlots...> allInputMsgStorage;
#include <tuple>
#include <string>

struct Timer
{};

template<class InputMsgType>
class StorageSlot {
public:

    template<class ArgType>
    StorageSlot(ArgType&& input)
        : readFlag(false),
        writeFlag(false),
        storageField(std::forward<ArgType>(input))
    //TODO initialize timer with period, get period from CAN
    {}

    InputMsgType storageField; //the field that it is being stored into
    bool readFlag; //the flag that checks if it is being read into
    bool writeFlag; //flag that checks if it is being written into
    Timer StorageSlotTimer; //the timer that checks the read and write flag
};


template <class... AllInputTypes>
class PostOffice {
public:

    //
    // note - perfect forwarding
    //
    template<class...Args>
    PostOffice(Args&&... allInputTypes)
    : allInputMsgStorage(std::forward<Args>(allInputTypes)...)
    {

    }

protected:
    std::tuple<StorageSlot<AllInputTypes>...> allInputMsgStorage; //TODO: Will this work? - yes
};





int main()
{
    PostOffice<int, double, std::string> po(10, 20.1, "foo");

}