[英]C++ - Can I make unspecified templated type (a.k.a higher-order type) as template argument?
For example, let's imagine that we need to write a function that allows user to fetch data from a remote server, and it will return a container that holds all data it fetched.例如,假设我们需要编写一个 function 允许用户从远程服务器获取数据,它会返回一个容器来保存它获取的所有数据。 For simplicity, let's consider it is synchronized, thus doesn't have any
co_await
, co_yield
fancy staffs.为简单起见,让我们认为它是同步的,因此没有任何
co_await
, co_yield
花哨的员工。
I hope the user can choses which container the function uses.我希望用户可以选择 function 使用哪个容器。 For example, the function can use a
std::vector
, a std::list
, or even a container writen by the user to store data.例如,function 可以使用
std::vector
、 std::list
甚至用户编写的容器来存储数据。
Quickly I realized the function probably can be implemented by C++ templates, but soon I found out the templated type should be a unspecified type rather than a ordinary type, that is, the user should write fetchData<std::list>
, instead of fetchData<std::list<Data>>
.很快我意识到 function 可能可以通过 C++ 模板实现,但很快我发现模板类型应该是未指定类型而不是普通类型,即用户应该写
fetchData<std::list>
,而不是fetchData<std::list<Data>>
。 I我
tried writing following snippets but it doesn't compile successfully.尝试编写以下代码段,但编译不成功。 Did I write it in awrong way, or it is just impossible to use unspecified templated type in C++.
我是不是写错了,或者在 C++ 中使用未指定的模板类型是不可能的。
here is the snippet I wrote:这是我写的片段:
template<class T>
void fetchFromServer(T<std::string> &output);
template<>
void fetchFromServer(std::list<std::string> &output)
{
// Use a std::list to hold data.
}
template<>
void fetchFromServer(std::vector<std::string> &output)
{
// Use a std::vector to hold data.
}
// User can specified his own version of `fetchFromServer`.
int main()
{
std::list<std::string> lst;
fetchFromServer<std::list>(list);
std::vector<std::string> vctr;
fetchFromServer<std::vector>(vctr);
}
It is sort of tricky because of the parameters.由于参数,这有点棘手。 But you can do this:
但是你可以这样做:
#include <vector>
#include <list>
#include <string>
template< template <typename T, typename... > class C, typename... Args >
void fetchFromServer(C<std::string,Args... > &output);
template<>
void fetchFromServer(std::list<std::string> &output)
{
// Use a std::list to hold data.
}
template<>
void fetchFromServer(std::vector<std::string> &output)
{
// Use a std::vector to hold data.
}
// User can specified his own version of `fetchFromServer`.
int main()
{
std::list<std::string> lst;
fetchFromServer<std::list>(lst);
std::vector<std::string> vctr;
fetchFromServer<std::vector>(vctr);
}
Code: https://godbolt.org/z/TanW9KjPf代码: https://godbolt.org/z/TanW9KjPf
In fact, you don't need the two specializations at all.事实上,你根本不需要这两个专业。 You can do this:
你可以这样做:
template< template <typename T, typename... > class C, typename... Args >
void fetchFromServer(C<std::string,Args... > &output) {
output.push_back( "1" );
output.push_back( "2" );
output.push_back( "3" );
}
int main()
{
std::list<std::string> lst;
fetchFromServer(lst);
std::vector<std::string> vctr;
fetchFromServer(vctr);
}
Code: https://godbolt.org/z/KMGGvW8b1代码: https://godbolt.org/z/KMGGvW8b1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.