gpt4 book ai didi

c++ - 特化和模板模板参数

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:59:25 25 4
gpt4 key购买 nike

我有以下内容:

template <template <typename, typename> class C, class T, class A >
class TTCTest
{
public:
TTCTest(C<T, A> parameter) { /* ... some stuff... */ }
C<T, A> _collection;
};

我想确保模板仅在 T 类和 A 类属于特定类型(分别为路径和分配器)的情况下实例化。

例如:

...
list<path, allocator<path> > list1;
list<int, allocator<int> > list2;

TTCTest<list> testvar(list1); // ...should work
TTCTest<list> testvar(list2); // ...should not work
...

这可能吗,语法是什么?

问候,上校

最佳答案

您可以通过部分特化来做到这一点,在这种情况下您无法为非特化案例提供实现。
例如:

template <template <typename, typename> class C, class T, class A >
class TTCTest;

template <template <typename, typename> class C>
class TTCTest<C, path, allocator<path> >
{
// ...
};

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

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