gpt4 book ai didi

c++ - 模板模板可变参数特化

转载 作者:行者123 更新时间:2023-11-30 05:13:18 26 4
gpt4 key购买 nike

我想知道是否可以通过以下方式专门化模板:

template<typename... Ts>
class Typelist { };

template<template<typename> typename... TTs>
class Typelist<TTs<typename>> { }; //I don't know if that is the right syntax, at least the compiler doesn't complain

我希望它作为模板类型列表和非模板类型列表工作:

template<typename T>
class AT {};

template<typename T>
class BT {};

template<typename T>
class CT {};

int main() {

using tl1 = Typelist<int, float, double>; //works fine
using tl2 = Typelist<AT, BT, CT>; //gives an error

}

编辑:

如果我将第二个 Typelist 声明为一个单独的类型,它就可以工作...

template<template<typename> typename... TTs>
class Typelist2 { };

//...
using tl2 = Typelist2<AT, BT, CT>; //compiler doesn't complain and it works fine

我想知道是否有可能在这两种情况下都只使用 Typelist,这样 Typelist2 就不必是一个单独的类型。

谁能帮帮我?

最佳答案

I was wondering though if it was possible to use only Typelist for both cases so that Typelist2 doesn't have to be a seperate type.

我不这么认为。

因为 TypeList定义为 template<typename... Ts>template<template<typename> typename... TTs> ;这两个定义不能一起工作。

我能想到的最好的帮助你的是定义一个 TypeList简单类型的基础版本

template <typename ... Ts>
struct TypeList
{
// here you can use Ts...
};

和如下所示的容器特化(其中只有一个类型)

template <template <typename> class ... Tts, typename ... Ts>
struct TypeList<Tts<Ts>...>
{
// here you can use Ts... and Tts...
};

在哪里可以使用 TsTts .

但这不是一个很好的解决方案,因为您无法定义容器 TypeList就像

TypeList<AT, BT, CT> tcl;

但您必须按如下方式添加包含的虚拟 (?) 类型

TypeList<AT<int>, BT<float>, CT<double>> tl2;

另一个问题是你不能混合它们,所以

TypeList<AT<int>, BT<float>, double> tl;

调用 TypeList 的基础版本(无容器) .

关于c++ - 模板模板可变参数特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44003571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com