我可以使用std :: vector作为模板参数,还是需要std :: vector< T>? [英] Can I use std::vector as a template parameter or does it need to be std::vector<T>?

查看:1514
本文介绍了我可以使用std :: vector作为模板参数,还是需要std :: vector< T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个简单的问题,但我只是找不到答案。

I know this is a simple question but I just could not find the answer.

我想这样做,而不是用std :: vector最终我希望它是std :: shared_ptr或std :: weak_ptr:

I am trying to do something like this but instead of with std::vector ultimately I want it to be std::shared_ptr or std::weak_ptr:

template <int dim, class ChunkClass, class PtrClass>
class BaseChunkWindow : public IChunkWindow<BaseChunkWindow<dim, ChunkClass, PtrClass>, IChunk<ChunkClass>> {
public:
...
private:
PtrClass< IChunk<ChunkClass> > ptr;  <-- compiler doesn't like this line, however IChunk<ChunkClass>* works
};


推荐答案

这取决于你传递给什么,if你试图实例化的模板作为参数一个类模板接受2(或在C ++ 11可变数量)类,然后你可以传递std :: vector到那个。但在大多数情况下,模板需要类作为参数,并且不能传递类模板std :: vector。

It depends on what you are passing it to, if the template you're trying to instantiate takes as a parameter a class template accepting 2 (or in c++11 a variadic number of) classes then you can pass std::vector to that. In most cases however, templates require classes as parameters and you cannot pass the class template std::vector.

    template <class T>
    struct gimme_a_type{};

    template <template <class,class> class T>
    struct gimme_a_template{};

    gimme_a_type<std::vector> //<-- does not compile, expected a type, got a template
    gimme_a_type<std::vector<int> > //<-- compiles, expected a type, got a type
    gimme_a_template<std::vector> //<-- compiles, expected a template, got a template that has the correct signature
    gimme_a_template<std::vector<int> > //<-- does not compile, expected a template, got a type

编辑,使用类模板作为模板参数有困难。在你要传递的类模板中有默认参数时,匹配参数的数量实际上很难做(在我们的例子中为 std :: vector )。
请注意,上面的示例需要一个类模板,它需要2个类,而不只是一个。这是因为 std :: vector 有两个参数,第二个默认为 std :: allocator< T>

In response to your edit, there are difficulties to using class templates as template parameters. Matching the number of parameters exactly is actually difficult to do when you have default arguments in the class template you're trying to pass (std::vector in our case). Notice that the example above required a class template that takes 2 classes, not just one. This is because std::vector takes two parameters, the second is just defaulted to std::allocator<T> for us.

以下示例演示了此问题:

The following example demonstrates the issue:

    template <template <class, class> class Tem>
    struct A
    {
        Tem<int> v; //<-- fails to compile on gcc, Tem takes two parameters
        Tem<int, std::allocator<int> >; //<-- compiles, but requires a priori knowledge of Tem
    };

    template <template <class...> class Tem>
    struct A2
    {
      Tem<int> v; //<-- This C++11 example will work, but still isn't perfect.
    };

C ++ 11示例更好,但如果有人传递了一个具有签名 template< class,bool = false>类A3 再次失败,因为 A2 需要可变数量的类,而不是可变数量的whatevers。所以即使 A3 可能是一个有效的实例化,你不能将该类传递给 A2 。 >
解决方案是总是使用模板参数列表中的类型,并使用 std :: integral_constant 包装器模板传递整数常量。

The C++11 example is better, but if someone passed a class that has as a signature template <class, bool = false> class A3 it fails again because A2 requires a variadic number of classes, not a variadic number of whatevers. So even though A3<int> could be a valid instantiation you couldn't pass that class to A2.
The solution there is to always use types in template parameter lists and use the std::integral_constant wrapper template to pass integral constants around.

这篇关于我可以使用std :: vector作为模板参数,还是需要std :: vector&lt; T&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆