函数参数包的行为
Behavior of a function parameter pack
template<typename... T>
void foo(T... args);
在上面的例子中,T
是根据标准的§14.5.3 - (4.1)展开的。
§14.5.3 - (4.1) — In a function parameter pack (8.3.5); the pattern is the parameter-declaration without the ellipsis.
这里到底发生了什么?假设我用 3 个整数调用函数。
foo(1, 2, 3);
参数包是否展开如
foo(int arg1, int arg2, int arg3);
其中 arg1、arg2 和 arg3 只是编译器赋予的任意名称?
标准怎么说"the pattern is the parameter-declaration without the ellipsis"
我解释的另一种方式是 args
获取单个参数-declartion.Is args
获取其自己的类型?我试过
std::cout << typeid(args).name;
但这不起作用,并且会引发编译器错误。所以我可以假设它没有自己的类型。有人能 "dumb down" 这里到底发生了什么,以及函数参数包的行为吗?
好的,我想我已经明白了。如果我错了,请纠正我。
当省略号位于模式右侧时,会发生参数扩展。我们这里的模式只是 T
:
foo(T... args);
标准规定其结果将是 parameter-declaration
。这会衰减为:
attribute-specifier-seqopt decl-specifier-seq declarator
attribute-specifier-seq
可以忽略
decl-specifier-seq
是类型,T
;
declarator
是 ... args
从语义上讲,这是一个函数参数包声明。没什么特别的。
再一次,我的好奇心让我有点发疯了……呵呵。
template<typename... T>
void foo(T... args);
在上面的例子中,T
是根据标准的§14.5.3 - (4.1)展开的。
§14.5.3 - (4.1) — In a function parameter pack (8.3.5); the pattern is the parameter-declaration without the ellipsis.
这里到底发生了什么?假设我用 3 个整数调用函数。
foo(1, 2, 3);
参数包是否展开如
foo(int arg1, int arg2, int arg3);
其中 arg1、arg2 和 arg3 只是编译器赋予的任意名称?
标准怎么说"the pattern is the parameter-declaration without the ellipsis"
我解释的另一种方式是 args
获取单个参数-declartion.Is args
获取其自己的类型?我试过
std::cout << typeid(args).name;
但这不起作用,并且会引发编译器错误。所以我可以假设它没有自己的类型。有人能 "dumb down" 这里到底发生了什么,以及函数参数包的行为吗?
好的,我想我已经明白了。如果我错了,请纠正我。
当省略号位于模式右侧时,会发生参数扩展。我们这里的模式只是 T
:
foo(T... args);
标准规定其结果将是 parameter-declaration
。这会衰减为:
attribute-specifier-seqopt decl-specifier-seq declarator
attribute-specifier-seq
可以忽略
decl-specifier-seq
是类型,T
;
declarator
是 ... args
从语义上讲,这是一个函数参数包声明。没什么特别的。
再一次,我的好奇心让我有点发疯了……呵呵。