简体   繁体   English

在 c++ 中将容器类型作为模板的类型名传递

[英]Pass a container type as the typename of a template in c++

I'm a beginner at templates in C++ and I would like to know if it's possible to pass a container to the typename of a template function, here is what I'm trying to do:我是 C++ 模板的初学者,我想知道是否可以将容器传递给模板 function 的类型名,这就是我想要做的:

template <typename T>
int find_size(const T<int> t)
{
    return (t.size());
}

int main(void)
{
    std::array<int, 10> test;
    for (int i = 0; i < 10; i++)
    {
        test[i] = i;
    }
    findsize(test);
}

When I'm compiling I get an error saying that T isn't a template.当我编译时,我收到一条错误消息,说 T 不是模板。 Is it possible to pass the template of a container to the template of a function?是否可以将容器的模板传递给 function 的模板?

With minimum changes to make it work, your code could be this:只需进行最少的更改即可使其正常工作,您的代码可能是这样的:

#include <array>

template <typename T>
int find_size(const T& t)
{
    return (t.size());
}

int main(void)
{
    std::array<int, 10> test;
    for (int i = 0; i < 10; i++)
    {
        test[i] = i;
    }
    find_size(test);
}

basically I want my function to be able to take an abritary type of container基本上我希望我的 function 能够携带 abritary 类型的容器

Thats exactly what the above does.这正是上面所做的。 It works for any container type T that has a size() .它适用于任何具有size()的容器类型T


If you actually want to parametrize find_size on a template rather than a type, then you can use a template template parameter:如果您实际上想在模板而不是类型上对find_size进行参数化,则可以使用模板模板参数:

#include <array>

template <template<class,std::size_t> class C>
int find_size(const C<int,10>& t)
{
    return (t.size());
}

int main(void)
{
    std::array<int, 10> test;
    for (int i = 0; i < 10; i++)
    {
        test[i] = i;
    }
    find_size<std::array>(test);
}

However, using this is either more complicated than illustrated here, or of more limited use than the above: For the function parameter you need a type not just a template, and this find_size will only work with a template C that has 2 parameters, one type and one non-type parameter of type size_t (and I am actually not aware of any other container but std::array with that template parameters).但是,使用它要么比这里说明的更复杂,要么比上面的使用更有限:对于 function 参数,您需要一个类型而不仅仅是一个模板,并且这个find_size仅适用于具有 2 个参数的模板C ,一个type 和一个size_t类型的非类型参数(实际上我不知道任何其他容器,但std::array具有该模板参数)。


TL;DR: This is not a use case where a template template parameter is needed. TL;DR:这不是需要模板模板参数的用例。

Is it possible to pass the template of a container to the template of a function?是否可以将容器的模板传递给 function 的模板?

Yes, but you don't need it here.是的,但你在这里不需要它。

You can pass your type to your templatized function.您可以将您的类型传递给您的模板化 function。 The compiler will check if the calls to this function are valid or not (ie: it will check if each call to your function is done with an object that has a size() method).`编译器将检查对此 function 的调用是否有效(即:它将检查对 function 的每次调用是否使用具有size()的 object 方法完成)。

I fixed your code, try if this works (untested)我修复了你的代码,试试这是否有效(未经测试)

template <typename T>
int find_size(const T &v)
{
    return int(v.size());
}

int main()
{
    std::array<int, 10> test;
    for (int i = 0; i < 10; i++)
    {
        test[i] = i;
    }
    find_size(test);
    return 0;
}

T is a type name, so calling.size() doesn't really make sense here. T 是一个类型名称,所以 call.size() 在这里没有意义。

Why are you trying to use templates if you want a parameter with a type?如果你想要一个类型的参数,你为什么要尝试使用模板? You could type cast your return.你可以键入 cast 你的回报。

try尝试

template <typename T>
int find_size(T a)
{
return (int) a.size();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM