Syntax issue when populating an array with a fold expression

后端 未结 2 893
-上瘾入骨i
-上瘾入骨i 2021-02-19 07:12

Yes, I can use std::initializer_list. Yes, even easier, I can do aggregate initialization. But how does this work? I can\'t seem to fold my head around C++17\'s fol

相关标签:
2条回答
  • 2021-02-19 07:41

    Since the comma operator is left-associative, you would ideally use a left unary fold:

    (...,void(arr[i++] = pack))
    

    The cast to void is to make sure that the built-in comma operator is used. In this case, the handedness doesn't actually matter.

    0 讨论(0)
  • 2021-02-19 07:43

    You need to fold with the comma operator, which also solves the sequencing problem.

    (void(arr[i++] = pack) , ...);
    
    0 讨论(0)
提交回复
热议问题