模板可变参数函数:具有任意数量构造函数参数的任意数量的类 [英] template variadic function: arbitrary number of classes with arbitrary number of constructor parameters

查看:47
本文介绍了模板可变参数函数:具有任意数量构造函数参数的任意数量的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定正确的名称是什么,到目前为止我理解使用可变参数模板参数的概念(例如,与 std::initializer_list 相比).

所以让我们假设有任意数量的类 k 和任意数量的参数 i 依赖于每个类 k 构造函数.

我知道我可以使用可变参数模板构造任何 k 类:

template void make_class(Args... args){汽车 k = T(args...);}

但是,我希望允许创建多个类型不同并且很可能具有不同Args的类.此外,我想迭代正在构造的类 k.大致意思:

template void make_classes(Classes...classes){for(自动类型:类){make_class(args...);//???}}

  1. 这可能吗?
  2. 如果不可能,是否有任何潜在的解决方法?
  3. 这种模式/方法有名字吗?这是parameter_pack吗?

如有重复,我们深表歉意.

解决方案

您需要通过在元组中传递每个类的参数来描述它们,否则对于具有多个构造函数的类,结果可能不明确(并注意大多数类至少有一个默认构造函数和复制构造函数).

完成,这很容易写:

templatevoid make_classes(Args&&... args) {auto k = std::tuple{std::make_from_tuple(std::forward(args))...};}

用法:

make_classes, std::string, std::vector>(std::forward_as_tuple(42, 3.14f),std::forward_as_tuple("你好"),std::forward_as_tuple(5, 99.99));

示例.

请注意,make_from_tuple 是 C++17 库的补充;如果您的图书馆没有它,您可以复制示例实现.>

I am unsure of what the proper name is, so far I comprehend the notion of using variadic template arguments (e.g., in comparison to std::initializer_list).

So let's assume there is an arbitrary number of classes k and an arbitrary number of parameters i which is dependant on each class k constructor.

I know that I may construct any class k using a variadic template:

template <typename T, typename... Args>
void make_class(Args... args)
{
    auto k = T(args...);
}

However I want to allow creation of multiple classes which are not of the same type and most likely have different Args. Furthermore, I want to iterate the classes k being constructed. Something along the lines:

template <typename T, typename... Classes, typename Args>
void make_classes(Classes... classes)
{
    for (auto type : classes) {
        make_class<T>(args...); //???
    }   
}

  1. Is this possible?
  2. If not possible, are there any potential work-arounds?
  3. Is there a name for such a pattern/approach? Is this a parameter_pack?

Apologies if this is a duplicate.

解决方案

You'll need to delineate the arguments for each class by passing them in a tuple, since otherwise the result could be ambiguous for classes with more than one constructor (and note that most classes have at least a default constructor and copy constructor).

That done, this is easy to write:

template<class... Classes, class... Args>
void make_classes(Args&&... args) {
    auto k = std::tuple<Classes...>{
        std::make_from_tuple<Classes>(std::forward<Args>(args))...};
}

Usage:

make_classes<std::pair<int, float>, std::string, std::vector<double>>(
    std::forward_as_tuple(42, 3.14f),
    std::forward_as_tuple("hello"),
    std::forward_as_tuple(5, 99.99));

Example.

Note that make_from_tuple is a C++17 library addition; if your library doesn't have it you can copy the example implementation.

这篇关于模板可变参数函数:具有任意数量构造函数参数的任意数量的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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