C ++ 11可变参数模板和以逗号分隔的表达式对等 [英] C++11 variadic templates and comma-separated expressions equivalence
本文介绍了C ++ 11可变参数模板和以逗号分隔的表达式对等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在可变参数模板中,...运算符将参数包扩展为一系列逗号分隔的参数(以最简单的形式)。我的问题是:为什么用逗号分隔的多个参数调用some_function()并用...运算符调用它不会呢?
In a variadic template the ... operator expands a parameter pack into a series of comma-separated arguments (in the simplest form). My question is: how come that calling some_function() for multiple arguments comma-separated works and calling it with the ... operator doesn't?
我在说关于此代码:
template<typename... Args> inline void expand(Args&&... args)
{
some_function(22),some_function(32); // Works
some_function(args)...; // Doesn't work - ERROR
}
这两行不会产生
推荐答案
因为在第一种情况下,您没有用逗号分隔的参数,而是使用了逗号-
Because in the first case you don't have comma-separated arguments but you are instead using the comma-operator, a totally different beast.
您可以递归实现 expand
函数:
inline void expand() {}
template<typename T, typename... Args>
inline void expand(T&& head, Args&&... tail)
{
some_function(head);
expand(tail...);
}
这篇关于C ++ 11可变参数模板和以逗号分隔的表达式对等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文