“... ...”的含义是什么意思。令牌?即参数包上的双省略号运算符 [英] What is the meaning of "... ..." token? i.e. double ellipsis operator on parameter pack

查看:280
本文介绍了“... ...”的含义是什么意思。令牌?即参数包上的双省略号运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览gcc当前实现的新C ++ 11头文件时,我偶然发现了......令牌。您可以通过[通过ideone.com]检查以下代码编译正确

  template< typename T> 
struct X
{/ * ... * /};

template< typename T,typename ... U>
struct X< T(U ......)> //这行是重要的一个
{/ * ... * /};

那么,这个标记的含义是什么?



编辑:看起来像SO修剪......问题标题为...,我的确的意思是......。 :)

解决方案

每个奇怪的实例都与普通的单个省略号的情况配对。

 模板< typename _Res,typename ... _ArgTypes> 
struct _Weak_result_type_impl< _Res(_ArgTypes ...)>
{typedef _Res result_type; };

template< typename _Res,typename ... _ArgTypes>
struct _Weak_result_type_impl< _Res(_ArgTypes ......)>
{typedef _Res result_type; };

template< typename _Res,typename ... _ArgTypes>
struct _Weak_result_type_impl< _Res(_ArgTypes ...)const>
{typedef _Res result_type; };

template< typename _Res,typename ... _ArgTypes>
struct _Weak_result_type_impl< _Res(_ArgTypes ......)const>
{typedef _Res result_type; };

我的猜测是双省略号的意义类似于 _ArgTypes ..



/ideone.com/8TQAa\">支持这个理论的测试我认为我们有一个新的赢家为最糟糕的伪操作员。



修改:这似乎是一致的。 §8.3.5/ 3描述了一种形成参数列表的方法:


parameter-declaration-list opt ... opt


因此,双省略号由一个参数声明列表一个参数包,后跟另一个省略号。



逗号是纯可选的; §8.3.5/ 4表示


在句法上正确,且...不是抽象声明符的一部分, ,...与...同义。


declarator ,但Johannes提出了一个好处,他们指的是参数声明中的抽象声明。我不知道为什么他们没有说参数声明的一部分,为什么这句话不只是一个信息性的注释...



此外, va_begin()< cstdarg> 中需要一个参数,因此原型 f ..) C ++特别允许使用。


$ b

根据请求,此处显示了双重省略号:

  #include< cstdio> 
#include< string>

template<类型名T>
T const& printf_helper(T const& x)
{return x; }

char const * printf_helper(std :: string const& x)
{return x.c_str(); }

template< typename ... Req,typename ... Given>
int wrap_printf(int(* fn)(Req ... ...),Given ... args){
return fn(printf_helper(args)...)
}

int main(){
wrap_printf(& std :: printf,Hello%s\\\
,std :: string(world! );
wrap_printf(& std :: fprintf,stderr,std :: string(Error%d),5);
}


While browsing through gcc's current implementation of new C++11 headers, I stumbled upon "......" token. You can check, that the following code compiles fine [via ideone.com].

template <typename T>
struct X
{ /* ... */ };

template <typename T, typename ... U>
struct X<T(U......)> // this line is the important one
{ /* ... */ };

So, what is the meaning of this token?

edit: Looks like SO trimmed "......" in question title to "...", I did really mean "......" . :)

解决方案

Every instance of that oddity is paired with a case of a regular single ellipsis.

  template<typename _Res, typename... _ArgTypes>
    struct _Weak_result_type_impl<_Res(_ArgTypes...)>
    { typedef _Res result_type; };

  template<typename _Res, typename... _ArgTypes>
    struct _Weak_result_type_impl<_Res(_ArgTypes......)>
    { typedef _Res result_type; };

  template<typename _Res, typename... _ArgTypes>
    struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
    { typedef _Res result_type; };

  template<typename _Res, typename... _ArgTypes>
    struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
    { typedef _Res result_type; };

My guess is that the double ellipsis is similar in meaning to _ArgTypes..., ..., i.e. a variadic template expansion followed by a C-style varargs list.

Here's a test supporting that theory… I think we have a new winner for worst pseudo-operator ever.

Edit: This does appear to be conformant. §8.3.5/3 describes one way to form the parameter list as

parameter-declaration-listopt ...opt

So the double-ellipsis is formed by a parameter-declaration-list ending with a parameter pack, followed by another ellipsis.

The comma is purely optional; §8.3.5/4 does say

Where syntactically correct and where "..." is not part of an abstract-declarator, ", ..." is synonymous with "...".

This is within an abstract-declarator, [edit] but Johannes makes a good point that they are referring to an abstract-declarator within a parameter-declaration. I wonder why they didn't say "part of a parameter-declaration," and why that sentence isn't just an informative note…

Furthermore, va_begin() in <cstdarg> requires a parameter before the varargs list, so the prototype f(...) specifically allowed by C++ is useless. Cross-referencing with C99, it is illegal in plain C. So, this is most bizarre.

Usage note

By request, here is a demonstration of the double ellipsis:

#include <cstdio>
#include <string>

template< typename T >
T const &printf_helper( T const &x )
    { return x; }

char const *printf_helper( std::string const &x )
    { return x.c_str(); }

template< typename ... Req, typename ... Given >
int wrap_printf( int (*fn)( Req... ... ), Given ... args ) {
    return fn( printf_helper( args ) ... );
}

int main() {
    wrap_printf( &std::printf, "Hello %s\n", std::string( "world!" ) );
    wrap_printf( &std::fprintf, stderr, std::string( "Error %d" ), 5 );
}

这篇关于“... ...”的含义是什么意思。令牌?即参数包上的双省略号运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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