“...”的规则是什么,令牌在可变模板的上下文中? [英] What are the rules for the "..." token in the context of variadic templates?

查看:127
本文介绍了“...”的规则是什么,令牌在可变模板的上下文中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11中有这样的可变参数模板:

 类T,类... Args> 
unique_ptr< T> make_unique(args& ... args)
{
return unique_ptr T(new T(std :: forward< Args>(args)...));
}

有一些好奇心:表达式 std :: forward< Args>(args)... 使用 Args args 但只有一个 ... 令牌。此外 std :: forward 是一个非可变模板函数,只需要一个模板参数和一个参数。 (粗略)的语法规则是什么?如何推广?



另外:在函数实现中,省略号( ... )位于感兴趣表达式的结尾。有没有理由在模板参数列表和参数列表中,省略号在中间?

解决方案

可变参数模板,省略号 ... 用于解压缩模板参数包(如果它出现在表达式的右侧)(调用此表达式模式一会儿)。规则是 ... 的左侧重复—解压缩的模式(现在称为 表达式)由逗号分隔。



通过一些例子可以最好的理解。假设你有这个功能模板:

  template< typename ... T& 
void f(T ... args)
{
g(args ...); // pattern = args
h(x(args)...); // pattern = x(args)
m(y(args ...)); // pattern = args(as argument as y())
n(z T(args)...) // pattern = z< T>(args)
}

现在,函数传递 T {int,char,short} ,则每个函数调用扩展为: p>

  g(arg0,arg1,arg2); 
h(x(arg0),x(arg1),x(arg2));
m(y(arg0,arg1,arg2));
n(z< int>(arg0),z char(arg1),z short(arg2)

在您发布的代码中, std :: forward 遵循 n()函数调用所示的第四种模式。



code> x(args)... 和 y(args ...)




您可以使用 ... 初始化数组, p>

  struct data_info 
{
boost :: any data;
std :: size_t type_size;
};

std :: vector< data_info> v {{args,sizeof(T)} ...}; // pattern = {args,sizeof(T)}

,扩展为:

  std :: vector< data_info> v 
{
{arg0,sizeof(int)},
{arg1,sizeof(char)},
{arg2,sizeof(short)}
};






我只是意识到一个模式甚至可以包括访问说明符例如 public ,如以下示例所示:

  typename ... Mixins> 
struct mixture:public Mixins ... // pattern = public Mixins
{
// code
};

在此示例中,模式扩展为:

  struct mixture__instantiated:public Mixin0,public Mixin1,... public MixinN 

也就是说,混合从所有基类中公开



希望有帮助。


In C++11 there are variadic templates like this one:

template< class T, class... Args >
unique_ptr<T> make_unique( Args&&... args )
{
    return unique_ptr<T>(new T(std::forward<Args>(args)...));
}

There are some curiosities about this: The expression std::forward<Args>(args)... uses both Args and args but only one ... token. Furthermore std::forward is a non-variadic template function taking only one template parameter and one argument. What are the syntax rules for that (roughly)? How can it be generalized?

Also: In the function implementation the ellipsis (...) is at the end of the expression of interest. Is there a reason that in the template argument list and the parameter list the ellipsis is in the middle?

解决方案

In the context of variadic template, the ellipsis ... is used to unpack the template parameter pack if it appears on the right side of an expression (call this expression pattern for a moment). The rule is that whatever pattern is on the left side of ... is repeated — the unpacked patterns (call them expressions now) are separated by comma ,.

It can be best understood by some examples. Suppose you have this function template:

template<typename ...T>
void f(T ... args) 
{
   g( args... );        //pattern = args
   h( x(args)... );     //pattern = x(args)
   m( y(args...) );     //pattern = args (as argument to y())
   n( z<T>(args)... );  //pattern = z<T>(args)
}

Now if I call this function passing T as {int, char, short}, then each of the function call is expanded as:

g( arg0, arg1, arg2 );           
h( x(arg0), x(arg1), x(arg2) );
m( y(arg0, arg1, arg2) );
n( z<int>(arg0), z<char>(arg1), z<short>(arg2) );

In the code you posted, std::forward follows the fourth pattern illustrated by n() function call.

Note the difference between x(args)... and y(args...) above!


You can use ... to initialize an array also as:

struct data_info
{
     boost::any  data;
     std::size_t type_size;
};

std::vector<data_info> v{{args, sizeof(T)}...}; //pattern = {args, sizeof(T)}

which is expanded to this:

std::vector<data_info> v 
{ 
   {arg0, sizeof(int)},
   {arg1, sizeof(char)},
   {arg2, sizeof(short)}
};


I just realized a pattern could even include access specifier such as public, as shown in the following example:

template<typename ... Mixins>
struct mixture : public Mixins ...  //pattern = public Mixins
{
    //code
};

In this example, the pattern is expanded as:

struct mixture__instantiated : public Mixin0, public Mixin1, .. public MixinN  

That is, mixture derives publicly from all the base classes.

Hope that helps.

这篇关于“...”的规则是什么,令牌在可变模板的上下文中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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