(1)先舉一個不是很恰當?shù)睦咏o大家看一下折疊表達式具體是怎么使用的:
int sum_1_to_5()
{
return (1 + 2 + 3 + 4 + 5);
}
sum_1_to_5();
我們使用
template<typename ...Args>
int sum(Args&&... args)
{
return (args + ... + 0);
}
sum(1, 2, 3, 4, 5);
也就是說折疊表達式能夠自動的將指定的操作應用于形參包。(args + ... + 0)
等價于(1+2+3+4+5+0)
载佳。
折疊表達式在一定的程度上可以簡化代碼颤陶。
(2)語法
( pack op ... ) (1)
( ... op pack ) (2)
( pack op ... op init ) (3)
( init op ... op pack ) (4)
pack
指的是參數(shù)包,op
指的是具體的操作穴亏,init
指的是具體的初始值蜂挪。上述的語法等價于
(1) 稱為一元右折疊:unary right fold
(2) 稱為一元左折疊:unary left fold
(3) 稱為二元右折疊:binary right fold
(4) 稱為二元左折疊:binary left fold
上述的表達式等價于:
(E op ...) <=> (E1 op (... op (E N-1 op EN))) (1)
(... op E) <=> (((E1 op E2) op ...) op EN) (2)
(E op ... op I) <=> (E1 op (... op (EN?1 op (EN op I)))) (3)
(I op ... op E) <=> ((((I op E1) op E2) op ...) op EN) (4)
E 為有N個元素的參數(shù)包,op為具體的操作符嗓化,I為初始值棠涮。在(3),(4)中蟆湖,兩個op一定要是一樣的故爵。
需要比較值得重點關注的一點是,二元op的取值范圍為
+
隅津,-
诬垂,*
, /
伦仍, %
结窘, ^
, &
充蓝, |
隧枫, =
, <
谓苟, >
官脓, <<
, >>
涝焙, +=
卑笨, -=
, *=
仑撞, /=
赤兴, %=
, ^=
隧哮, &=
桶良, |=
, <<=
沮翔, >>=
陨帆, ==
, !=
, <=
歧譬, >=
岸浑, &&
, ||
瑰步,,
矢洲, .*
, ->*
缩焦。
再給大家兩個比較簡單的例子
#include <iostream>
#include <vector>
//(1) ( pack op ... )
template<class T, class... Args>
void push_back_vec(std::vector<T>& v, Args&&... args)
{
static_assert((std::is_constructible_v<T, Args&> && ...));
(v.push_back(args), ...);
}
//(4) (init op ... op pack)
template<class ...Args>
void printer(Args&&... args)
{
(std::cout << ... << args) << '\n';
}
int main()
{
std::vector<int> v;
push_back_vec(v, 6, 2, 45, 12);
for (int i : v) std::cout << i << ' '; //6 2 45 12
std::cout << "\n";
printer(1, 2, 3, "abc"); //123abc
return 0;
}
幾個需要注意的點:
- 只有三個運算符允許pack為空,
&&
读虏、||
和,
。&&
為true
袁滥,||
為false
盖桥,,
為void()
。
template <typename... Args>
auto empty_pack_unary_fold_operator_and(Args&&... args){
return (args && ...);
}
template <typename... Args>
auto empty_pack_unary_fold_operator_or(Args&&... args){
return (args || ...);
}
template <typename... Args>
auto empty_pack_unary_fold_operator_comma(Args&&... args){
return (args , ...);
}
TEST_CASE("a unary fold is used with a pack expansion of length zero", "fold expression"){
CHECK(true == empty_pack_unary_fold_operator_and());
CHECK(false == empty_pack_unary_fold_operator_or());
using void_type = decltype(empty_pack_unary_fold_operator_comma());
CHECK( std::is_void<void_type>::value );
}
- 注意運算符的優(yōu)先級题翻。