简体   繁体   English

如何编写一个采用通用C ++标准库容器的模板函数

[英]How can I write a template function that takes a generic C++ standard library container

I have a function that needs to be able to work for different container types, for example 例如,我有一个功能需要能够适用于不同的容器类型

void foo(const std::vector<bar>& param1, const std::vector<double>& params2)

and

void foo(const std::list<bar>& param1, const std::list<double>& params2)

where bar is a class that I've written. bar是我写的一个class The function body itself uses generic C++ standard library functions. 函数体本身使用通用的C ++标准库函数。

Is there a way I can templatise this? 有没有办法可以扼杀这个? I have tried 我努力了

template<typename T> void foo(const T<bar>&, const T<double>&)

But this gives the compiler error 但这会给编译器带来错误

error C2988: unrecognizable template declaration/definition 错误C2988:无法识别的模板声明/定义

I'm using MSVC2015. 我正在使用MSVC2015。

You should declare T as template template parameter to indicate that it's a template-name (and needs arguments to be instantiated), eg 您应该将T声明为模板模板参数,以指示它是模板名称(并且需要实例化参数),例如

template<template <typename...> class T> 
void foo(const T<bar>&, const T<double>&);

LIVE 生活

There are basically three solutions: 基本上有三种解决方案:

  1. Use template template arguments . 使用模板模板参数

  2. Use iterators (which is what the standard library itself uses) 使用迭代器(这是标准库本身使用的)

  3. Use different templates for different arguments, and let the compiler deduce the actual types. 对不同的参数使用不同的模板,让编译器推导出实际的类型。 With your function it would be something like 有了你的功能,它就像是

     template<typename T, typename U> void foo(const T&, const U&); 

Solution 2 is the recommended one if you just need to iterate over the contents of the container. 如果您只需要迭代容器的内容,建议使用解决方案2。 Solution 3 is the recommended one if you need access tot he container itself. 如果您需要访问容器本身,建议使用解决方案3。

So-called template template arguments will allow you to write the desired code (as also shown in other answers or over on CppReference ). 所谓的模板模板参数将允许您编写所需的代码(如其他答案或CppReference中所示 )。

Alternatively, you could also write generic functions that accept iterators . 或者,您也可以编写接受迭代器的泛型函数。 This is exactly how the functions in the <algorithm> are implemented. 这正是<algorithm>中的函数的实现方式。 I tend to believe that functions that accept iterators are actually more flexible as the caller can decide what range needs to be processed by the function. 我倾向于认为接受迭代器的函数实际上更灵活,因为调用者可以决定函数需要处理的范围。

暂无
暂无

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

相关问题 如何使用boost /标准库在C ++中读/写图像? - How can I read/write an image in C++ using just boost/standard library? 如何在C ++中编写一个带有可变数量的double向量的函数? - How can I write a function in C++ that takes variable number of vectors of double? 如何编写将 LinkedList&amp; 作为 C++ 中的输入的合并排序 function? - How can I write merge-sort function that takes LinkedList& as input in C++? 如何使用带有模板参数的成员 function 模板为 class 编写 c++ 概念? - How to write a c++ concept for class with member function template that takes a template argument? 如何覆盖 C++ 标准库 class function? - How can I override an C++ standard-library class function? C++ 标准模板库 (STL) 中的列表。 我制作了以下程序,但我不知道如何制作打印列表的功能 - List in C++ Standard Template Library (STL). I made the following program and I don't know how to make a function to print the list 如何用C ++编写一个将整数和字符串都作为参数并对其进行修改的泛型函数? - How to write a generic function in C++ that takes as arguments both integers and strings and modifies them? 如何在C ++标准库中启用哈希? - How can I enable hash in C++ Standard Library? C ++标准库中的模板模板参数? - Template Template Parameters in the C++ Standard Library? 如何在 C++ 中编写通用转换函数? - How to write a generic convert function in c++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM