简体   繁体   中英

clang 3.6 fold expression left/right

I'm trying the fold expression with clang 3.6 '--std=c++1z', but something I don't quite get. The function that I'm testing is:

auto minus = [](auto... args) { return (args - ...); };
...
std::cout << minus(10, 3, 2) << std::endl;

according to n4191 , I'm expecting it expands as a left fold to

(10 - 3) - 2

which gives result 5, however, the result is 9, which seems to be a right fold expansion, ie

10 - (3 - 2)

Am I missing anything or mis-understand n4191? Thanks

n4191 was revised by n4295 . According to that, an expression of the form (e op ...) is a unary right fold , and that is expanded as: E1 op (... op (EN-1 op EN)) , ie as a right fold expansion.

This does seem to be the reverse of what n4191 stated in terms of the fold direction. Clang 3.6 implements the n4295 proposal, as shown here .

... - args would be a unary left fold and expand in the direction you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM