小编Amr*_*der的帖子

获取每种参数包的大小?

经过漫长的一天不成功的网上搜索找到解决问题的实用解决方案后,我决定在这里发布我的问题,以澄清我的目标,我提供了这个简单的代码:

template<typename... types>
std::vector<SIZE_T> GetTypesSize()
{
    std::vector<SIZE_T> typesSizeContainer;
    typesSizeContainer.reserve(sizeof... (types)); 

    /*
     * What I want here is a mechanism to loop throw 
     * each element of the parameter pack to get its size 
     * then push it into typesSizeContainer.   
     * Something similar to :
     *
     *     for(auto& element : types...) {
     *         typesSizeContainer.push(sizeof(element));
     *     }
     * 
     */

    return std::move(typesSizeContainer);
}
Run Code Online (Sandbox Code Playgroud)

当我在这段代码中调用此函数模板时:

// platform x86
std::vector<SIZE_T> tempVactor;
tempVactor = GetTypesSize<char, short, int>(); 
Run Code Online (Sandbox Code Playgroud)

tempVactor应该是的元素{ 1, 2, 4 }.

任何建议或解决方案都相当可观.

c++ templates variadic

2
推荐指数
2
解决办法
932
查看次数

标签 统计

c++ ×1

templates ×1

variadic ×1