简体   繁体   English

C++ - 我可以将未指定的模板类型(又名高阶类型)作为模板参数吗?

[英]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_awaitco_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::vectorstd::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.

相关问题 C ++函数模板参数与模板化类型struct woes - C++ function template argument with templated type struct woes 返回类型为模板类型的C ++模板 - c++ template with return type as templated type C ++中的模板和高阶类 - Templates and higher-order kinds in C++ 在C ++模板函数中,我可以返回一个解除引用的参数类型吗? - In a C++ template function can I return a dereferenced argument type? 如何在C ++中的另一个模板函数中使用属于模板化类的嵌套类型? - How can I use a nested type belonging to a templated class in another template function in C++? 带有模板化类型的静态私有函数,作为C ++中的默认参数 - Static private function with templated type as a default argument in C++ 模板偏特化可以缩小 C++ 中的参数类型吗? - Can template partial specialization narrow the argument type in C++? C ++模板传入模板化类,并在以后指定模板类型 - C++ templates pass in a templated class and specify the template type later C ++:从类返回模板对象,模板类型不匹配? - C++: Returning a templated object from a class, template type mismatch? 模板化代码如何在C ++中找到类型为模板参数的字符串的长度? - How can templated code find the length of a string whose type is a template parameter, in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM