fold-expression

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

半城伤御伤魂 提交于 2019-12-31 16:51:37
问题 #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

What is a good alternative to this C++17 fold expression in C++14?

余生长醉 提交于 2019-12-19 21:47:46
问题 Here is a nice, succinct fold expression based lambda in C++17: #include <cstdint> using ::std::uint64_t; constexpr auto sumsquares = [](auto... n) { return ((n * n) + ...); }; // I want this to work. uint64_t foo(uint64_t x, uint64_t y, uint64_t z) { return sumsquares(x, y, z); } // And this too double bar(uint64_t x, double y) { return sumsquares(x, y); } I have this code I've written to do something similar in C++14, but it seems a lot more verbose and confusing than it should be. I'm

What is a good alternative to this C++17 fold expression in C++14?

☆樱花仙子☆ 提交于 2019-12-19 21:47:19
问题 Here is a nice, succinct fold expression based lambda in C++17: #include <cstdint> using ::std::uint64_t; constexpr auto sumsquares = [](auto... n) { return ((n * n) + ...); }; // I want this to work. uint64_t foo(uint64_t x, uint64_t y, uint64_t z) { return sumsquares(x, y, z); } // And this too double bar(uint64_t x, double y) { return sumsquares(x, y); } I have this code I've written to do something similar in C++14, but it seems a lot more verbose and confusing than it should be. I'm

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

爷,独闯天下 提交于 2019-12-18 08:27:43
问题 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)(); }), ...); ~~~~~~~~~~~~~~~~~~~~~~~~^~

Compiler error with a fold expression in enable_if_t

╄→гoц情女王★ 提交于 2019-12-11 06:47:31
问题 I have the following code, where I am using a fold expression to evaluate whether all pack parameters are convertible to the first function argument. For some reason it fails to compile on msvc when I make what seems like a very trivial change: #include <type_traits> #define TRY 1 #if TRY == 1 template<typename B, typename... Args, std::enable_if_t<((std::is_convertible_v<Args&, B&> && ...)), bool> = true> void fn(B b, Args...args) {} #else template<typename B, typename... Args, typename =

Order of Evaluation for Fold Expressions

半城伤御伤魂 提交于 2019-12-07 11:59:16
问题 Fold expressions seem to be a nice way to apply a function to each element of a tuple. However, if the applied function has side effects, the order of function invocations might be an important concern. Consider: #include <iostream> template<typename... Ts> void printStuff(Ts... args) { ( ([](auto&& v) { std::cout << v << " "; })(args), ... ); std::cout << '\n'; } int main() { printStuff("hello", 42, 1.5f); // expected output: hello 42 1.5 } This seems to work. But is the order of evaluation

Order of Evaluation for Fold Expressions

旧巷老猫 提交于 2019-12-06 01:35:37
Fold expressions seem to be a nice way to apply a function to each element of a tuple. However, if the applied function has side effects, the order of function invocations might be an important concern. Consider: #include <iostream> template<typename... Ts> void printStuff(Ts... args) { ( ([](auto&& v) { std::cout << v << " "; })(args), ... ); std::cout << '\n'; } int main() { printStuff("hello", 42, 1.5f); // expected output: hello 42 1.5 } This seems to work . But is the order of evaluation for the lambdas guaranteed here or could I end up with the values being flipped around in the output?

Associativity of fold-expressions

假如想象 提交于 2019-12-05 16:44:51
问题 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. 回答1: From the comment by @cpplearner, here's

Syntax issue when populating an array with a fold expression

删除回忆录丶 提交于 2019-12-05 15:21:13
问题 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 fold expressions. There aren't enough examples out there. Here's what I came up with: template<class T, std::size_t N> struct foo { T arr[N]; template<typename... Args> constexpr foo(Args&&... pack) { static_assert(sizeof...(pack) <= N, "Too many args"); std::size_t i = 0; (arr[i++] = ...); } }; int main() { foo<int, 5> a(1, 2, 3, 4, 5

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

旧巷老猫 提交于 2019-12-05 14:29:16
问题 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