C++ 为变量模板中给定的所有类型添加模板专门化

C++ 为变量模板中给定的所有类型添加模板专门化,c++,templates,C++,Templates,我有一个关于模板的大家庭作业,其中一部分是创建一个结构template struct Foo,它包含一个公共结构template struct Bar,它共享(在公共类型定义中)一个结构,其中我们必须包含一个公共方法模板static constepr size\u t count()如果在Foo和Bar或0的参数中给定了类型,则输出1。示例(应编译以下代码): 使用FooBar=Foo; 使用Foo1Type=FooBar::Bar::type; //foo1作为Foo和Bar的参数 静态断言(

我有一个关于模板的大家庭作业,其中一部分是创建一个结构
template struct Foo
,它包含一个公共结构
template struct Bar
,它共享(在公共
类型
定义中)一个结构,其中我们必须包含一个公共方法
模板static constepr size\u t count()
如果在
Foo
Bar
0
的参数中给定了类型,则输出
1
。示例(应编译以下代码):

使用FooBar=Foo;
使用Foo1Type=FooBar::Bar::type;
//foo1作为Foo和Bar的参数
静态断言(Foo1Type::count()==1);
//foo2作为Foo的参数,但不是Bar
静态断言(Foo1Type::count()==0);
使用Foo4Type=FooBar::Bar::type;
//foo4未作为Foo的参数提供
静态断言(Foo4Type::count()==0);
静态断言(Foo4Type::count()==0);
对我来说,它看起来相当核心(我对模板不熟悉,刚刚开始阅读它们),似乎我们必须通过可变模板参数迭代到
Foo
,并在迭代过程中以某种方式为内部结构
创建新的专门化。。。我从来没有见过这样的事情,所以我只能猜测如何做到这一点


那么,我对这个问题的思考是否很好,或者我应该以不同的方式来处理它?我将非常感谢您的帮助(不仅仅是完整的解决方案)-欢迎您提供任何有用的链接。

据我所知,您的内部功能只是:

return contain<Type, TBar>{} && contain<Type, TFoos...>{};
返回contain{}&&contain{};
而struct contain可以写为:

template <typename T, typename ... Ts> struct contain;

// Partial specializations
// Empty case
template <typename T> struct contain<T> : std::false_type {};

// Found
template <typename T, typename ... Tail>
struct contain<T, T, Tail...> : std::true_type {};

// Not found, iterate the list
template <typename T, typename Head, typename ... Tail>
struct contain<T, Head, Tail...> : contain<T, Tail...> {};
模板结构包含;
//部分专业
//空箱
模板结构包含:std::false_type{};
//发现
模板
结构包含:std::true_type{};
//未找到,请迭代列表
模板
结构包含:包含{};

您允许使用哪种C++标准版本?代码> CONTXPROP/CODEC>表示C++ 11或更高版本。@ MelaK47 C++标记是最新的C++标准。我甚至可以使用C++ 17。to@melak47:由于OP在没有消息的情况下使用
static\u assert
,因此它似乎是c++1z。
template <typename T, typename ... Ts> struct contain;

// Partial specializations
// Empty case
template <typename T> struct contain<T> : std::false_type {};

// Found
template <typename T, typename ... Tail>
struct contain<T, T, Tail...> : std::true_type {};

// Not found, iterate the list
template <typename T, typename Head, typename ... Tail>
struct contain<T, Head, Tail...> : contain<T, Tail...> {};