C++17:folder expression(折疊表達式)

(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;
}

幾個需要注意的點:

  1. 只有三個運算符允許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 );
}
  1. 注意運算符的優(yōu)先級。
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容