fold-expression

Clang and the binary fold expressions — The curse of the empty parameter pack

你。 提交于 2019-12-04 02:16:21
Specifically Clang 3.6.0, the one currently hosted by Coliru. All these snippets are called from : int main() { foo(); std::cout << "\n----\n"; foo(1, 2, 3); } The following code : template <class... Args> void foo(Args... args) { std::cout << ... << args; } Triggers the following compilation error : main.cpp:7:17: error: expected ';' after expression std::cout << ... << args; ^ ; main.cpp:7:15: error: expected expression std::cout << ... << args; ^ So I tried putting parentheses around the expression : (std::cout << ... << args); It works, but triggers a warning : main.cpp:7:6: warning:

Associativity of fold-expressions

℡╲_俬逩灬. 提交于 2019-12-04 01:30:20
N4191 proposed fold-expressions to C++. The definition there was that (args + ...) is a left-fold (i.e. (((a0 + a1) + a2) + ...) , and that (... + args) is a right-fold (i.e. (... + (a8 + (a9 + a10))) . However, the revised paper N4295 reversed the definitions of left and right unary folds. Question : what is the rationale? It seems more intuitive (at least when you are used to left-to-right alphabets) to evaluate (args + ...) from left-to-right. From the comment by @cpplearner, here's some archeology from std-discussion On Wed, Feb 4, 2015 at 1:30 AM, @T.C. wrote : In N4295, which was

How to make `short-circuit evaluation` also available in `fold expressions`?

五迷三道 提交于 2019-12-03 00:35:35
#include <type_traits> #define FORWARD(arg)\ std::forward<decltype(arg)>(arg) template<typename... Args> constexpr bool AndL(Args&&... args) { return (... && FORWARD(args)); } template<typename... Args> constexpr bool AndR(Args&&... args) { return (FORWARD(args) && ...); } int main() { bool* pb = nullptr; false && (*pb = true); // ok at runtime. AndL(false, (*pb = true)); // error at runtime! AndR(false, (*pb = true)); // error at runtime! } The traditional && operator supports short-circuit evaluation , so false && (*pb = true) will be ok at runtime, but the following two cases are not. How

Print spaces between each element using a fold expression

主宰稳场 提交于 2019-12-01 23:31:00
问题 I am using a fold expression to print elements in a variadic pack, but how do I get a space in between each element? Currently the output is "1 234", the desired output is "1 2 3 4" template<typename T, typename Comp = std::less<T> > struct Facility { template<T ... list> struct List { static void print() { } }; template<T head,T ... list> struct List<head,list...> { static void print() { std::cout<<"\""<<head<<" "; (std::cout<<...<<list); } }; }; template<int ... intlist> using IntList =

Print spaces between each element using a fold expression

偶尔善良 提交于 2019-12-01 21:19:16
I am using a fold expression to print elements in a variadic pack, but how do I get a space in between each element? Currently the output is "1 234", the desired output is "1 2 3 4" template<typename T, typename Comp = std::less<T> > struct Facility { template<T ... list> struct List { static void print() { } }; template<T head,T ... list> struct List<head,list...> { static void print() { std::cout<<"\""<<head<<" "; (std::cout<<...<<list); } }; }; template<int ... intlist> using IntList = typename Facility<int>::List<intlist...>; int main() { using List1 = IntList<1,2,3,4>; List1::print(); }

Expanding parameter pack into lambda with fold expression - gcc vs clang

依然范特西╮ 提交于 2019-12-01 17:20:16
Considering the following code snippet: template <typename TF> void post(TF){ } template <typename... TFs> struct funcs : TFs... { funcs(TFs... fs) : TFs{fs}... { } void call() { (post([&]{ static_cast<TFs&>(*this)(); }), ...); } }; clang++ 3.8+ successfully compiles the code . g++ 7.0 fails to compile with the following error: prog.cc: In lambda function: prog.cc:10:43: error: parameter packs not expanded with '...': (post([&]{ static_cast<TFs&>(*this)(); }), ...); ~~~~~~~~~~~~~~~~~~~~~~~~^~ prog.cc:10:43: note: 'TFs' prog.cc: In member function 'void funcs<TFs>::call()': prog.cc:10:13: error

C++17 fold expression in cout

戏子无情 提交于 2019-12-01 05:25:57
I am learning the new c++17 fold expression and I saw this code from c++17 fold expression . I would like to know why this code work : template<typename ...Args> void printer(Args&&... args) { (std::cout << ... << args) << '\n'; } but not this one : template<typename ...Args> void printer(Args&&... args) { (std::cout << args << ...) << '\n'; } which could seems logic too and would reverse the print order in my opinion. As seen on cppreference , binary folds can have the following two forms: Where E is the pack expression and I is the initialization expression . There is no binary fold that

C++17 fold expression in cout

爱⌒轻易说出口 提交于 2019-12-01 03:42:47
问题 I am learning the new c++17 fold expression and I saw this code from c++17 fold expression. I would like to know why this code work : template<typename ...Args> void printer(Args&&... args) { (std::cout << ... << args) << '\n'; } but not this one : template<typename ...Args> void printer(Args&&... args) { (std::cout << args << ...) << '\n'; } which could seems logic too and would reverse the print order in my opinion. 回答1: As seen on cppreference, binary folds can have the following two forms