用可变参数模板中的每个类型配对非类型

问题描述 投票:0回答:1

我有什么方法可以使用可变参数对吗?我的目标是为每种类型添加额外的信息。

类似的东西:

// non compilable code

// I want to pass an extra uint8_t with each type here.
template<template<typename EnumType, uint8_t Bitmask> typename... EnumsAndBitmasks>
void func(){
    // Unpack EnumsAndBitmasks and do something with pairs of each EnumType and it's Bitmask
}

// Call func like this
enum class Type1 : uint8_t;
enum class Type2 : uint8_t;
enum class Type3 : uint8_t;

func<<Type1,0x03>, <Type2,0x3C>, <Type3,0xC0>>();

这是非模板工作代码:我想通过提供返回的结构类型和每个EnumType及其位掩码作为unpack_from_single_byte函数的模板参数来概括该过程。

enum class EnumType1 : uint8_t { first, second };
enum class EnumType2 : uint8_t { first, second, third, forth };
enum class EnumType3 : uint8_t { first, second };

struct Unpacked
{
    EnumType1 var1;
    EnumType2 var2;
    EnumType3 var3;
};

Unpacked unpack_from_single_byte(uint8_t value)
{
        return { static_cast<EnumType1>(value & 0x01), 
                 static_cast<EnumType2>(value & 0x06), 
                 static_cast<EnumType3>(value & 0x0F) };
}

我有什么方法可以使用可变参数对吗?我的目标是为每种类型添加额外的信息。就像这样://不可编译的代码//我想通过每种类型传递一个额外的uint8_t ...

c++ templates variadic-templates
1个回答
1
投票

您可以添加一个间接级别。首先,引入标签:

© www.soinside.com 2019 - 2024. All rights reserved.