简体   繁体   English

Class 具有两个或更多模板重载

[英]Class with two or more template overloads

Suppose I had a class that I wanted to declare with two "overloads", one that accepted 1 template parameter and another that accepted 2 like the pseudo code below:假设我有一个 class 我想用两个“重载”声明,一个接受 1 个模板参数,另一个接受 2,如下面的伪代码:

template <typename I>
class B {
public:
};


template <typename F, typename I>
class B { 
};

such that B can be instantiated with only 1 or 2 parameters:这样B可以仅用 1 或 2 个参数实例化:

B<int> hello;
B<int, int> hello2;

What is the correct way to do this?这样做的正确方法是什么?

Simple answer:简单的回答:

template <typename... Args> // ellipsis makes Args a template parameter pack
class B
{
public:
};

int main()
{
    B<int> hello;
    B<int, int> hello2;
    B<int, int, double> hello3;
}

Now Args is called a template parameter pack .现在Args被称为模板参数包

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

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