解包枚举的可变参数包

huangapple go评论75阅读模式
英文:

Unpacking Variadic Parameter Pack of Enums

问题

以下是您要求的翻译:

更新:已编辑以修复工作示例中的编译问题。

我想要做类似以下的事情,以便该函数可以接受枚举类实例的列表或保存它们的结构体,但是“auto myFunc2<EList<Types...>>”的定义失败,出现“期望常数而不是E::A”的错误。

```cpp
#include <cstdint>
#include <iostream>

enum class E { A = 0, B = 1 };
template <E... Types> struct tl2 {};
using my_list_2 = tl2<E::A>;

template <E... Types>
auto myFunc2 = [] {
    std::cout << "Size: " << sizeof...(Types) << std::endl;
};

template <template <E...> typename EList, E... Types>
auto myFunc2<EList<Types...>> = [] {
    std::cout << "Size: " << sizeof...(Types) << std::endl;
};

int main() {
    myFunc2<E::A, E::B>();  // 此调用打印类型列表的大小
                          // 当注释掉myFunc2<Elist<Types..>>时可以正常工作

    // myFunc2<my_list_2>();      // 这会导致错误
}

如果将此转换为通用类型,则一切都会编译并按预期工作。例如:

#include <cstdint>
#include <iostream>

template <typename ... Types> struct tl
{
};
using my_list = tl <int, float, uint64_t>;

template <typename ... Types>
static constexpr auto myFunc2 = [] {
    std::cout << "Size: " << sizeof...(Types) << std::endl;
};

template <template <class...> class TL, typename ... Types>
static constexpr auto myFunc2<TL<Types...>> = [] {
    std::cout << "Size: " << sizeof...(Types) << std::endl;
};

int main() {
    myFunc2<int, uint64_t, bool, uint16_t>();
    myFunc2<my_list>();
}

这里发生了什么?枚举类在模板中的处理是否存在限制?

希望这可以帮助您理解问题所在。如果您需要更多信息或有其他问题,请随时提问。

英文:

Update: Edited to fix compilation in working example.

I want to do something like the following so the function can take in both a list of the enum class instances or a struct that holds them, but the definition of auto myFunc2&lt;EList&lt;Types...&gt;&gt; fails with expected a constant not E::A

#include &lt;cstdint&gt;
#include &lt;iostream&gt;

enum class E {A = 0, B=1};
template&lt;E... Types&gt; struct tl2 {};
using my_list_2 = tl2&lt;E::A&gt;;

template &lt;E ... Types&gt;
auto myFunc2 = [] {
    std::cout &lt;&lt; &quot;Size: &quot; &lt;&lt; sizeof...(Types) &lt;&lt; std::endl;     
};

template &lt;template&lt;E...&gt; typename EList, E... Types&gt;
auto myFunc2&lt;EList&lt;Types...&gt;&gt; = [] {
    std::cout &lt;&lt; &quot;Size: &quot; &lt;&lt; sizeof...(Types) &lt;&lt; std::endl;     
};

int main() {
    myFunc2&lt;E::A, E::B&gt;();  // This call prints size of typelist
                            //Works when myFunc2&lt;Elist&lt;Types..&gt;&gt; is commented out

    //myFunc2&lt;my_list_2&gt;();      //This breaks      
} 

If we convert this to general types then everything compiles and works as expected. For example:

#include &lt;cstdint&gt;
#include &lt;iostream&gt;

template &lt; typename ... Types &gt; struct tl
{
};
using my_list = tl &lt;int, float, uint64_t&gt;;

template &lt;typename ... Types&gt;
static constexpr auto myFunc2 = [] {
    std::cout &lt;&lt; &quot;Size: &quot; &lt;&lt; sizeof...(Types) &lt;&lt; std::endl;   
};

template &lt;template&lt;class...&gt; class TL, typename ... Types&gt;
static constexpr auto myFunc2&lt;TL&lt;Types...&gt;&gt; = [] {
    std::cout &lt;&lt; &quot;Size: &quot; &lt;&lt; sizeof...(Types) &lt;&lt; std::endl;   
};

int main() {
    myFunc2&lt;int,uint64_t,bool,uint16_t&gt;();
    myFunc2&lt;my_list&gt;();                    
} 

What is going on here? Is there a limitation in how enum classes can be handled as templates?

答案1

得分: 2

真正的函数模板可以做到这一点(通常需要额外的辅助来推导参数包):

```cpp
template <typename... Types>
void myFunc2() {
    std::cout << "Size: " << sizeof...(Types) << std::endl;     
}

namespace detail {
template <template<typename...> typename EList, typename... Types>
void myFunc2(EList<Types...>) {
    std::cout << "Size: " << sizeof...(Types) << std::endl;     
}
}

template <typename EList>
void myFunc2() {
    detail::myFunc2(EList());
}

这种方法使得“一个模板”能够接受不同种类的模板参数,因为具有错误种类的模板参数的模板将在重载解析过程中被忽略。


<details>
<summary>英文:</summary>

True **function** templates can do this (with the usual extra helper for deducing a pack):

template <E ... Types>
void myFunc2() {
std::cout << "Size: " << sizeof...(Types) << std::endl;
}

namespace detail {
template <template<E...> typename EList, E... Types>
void myFunc2(EList<Types...>>) {
std::cout << "Size: " << sizeof...(Types) << std::endl;
}
}

template <typename EList>
void myFunc2() {
detail::myFunc2(EList());
}

This approach allows “one template” to accept template arguments of different **kinds** because the templates with the wrong kinds of template parameters will simply be ignored during overload resolution.

</details>



huangapple
  • 本文由 发表于 2023年6月16日 04:15:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定