generic-lambda

Enabling `-std=c++14` flag in Code::Blocks

北城余情 提交于 2019-12-17 23:24:00
问题 I have installed Code::Blocks for Windows and want to compile C++14 code like generic lambdas but the binary version of Code::Blocks that I've installed from codeblocks.org doesn't support the flag -std=c++14 . How do I update the compiler and enable -std=c++14 flag for Code::Blocks? 回答1: To compile your source code using C++14 in Code::Blocks, you, first of all, need to download and install a compiler that supports C++14 features. Here’s how you can do it on Windows: Download MinGW from here

clang vs gcc: variadic lambda captures

杀马特。学长 韩版系。学妹 提交于 2019-12-08 15:21:00
问题 I am trying to capture a variadic lambda argument inside a inner lambda and use it there. As an example, consider this code: int main () { auto first = [&] (auto&&... one) { auto second = [&] (auto&&... two) { return ((one * two) + ...); }; return second(one...); }; return first(5); } This works with gcc9 but fails with clang8 (https://godbolt.org/z/i2K9cK). A way to make the code compile is to explicitly capture [&one...] , but i was wondering whether this is a bug in clang. Also interesting

Should non-capturing generic lambdas decay to function pointers?

▼魔方 西西 提交于 2019-12-05 05:37:53
Consider the following code: int main() { auto l = [](auto){}; void(*p)(int) = l; } It works just fine both with GCC and clang . Let's consider the following slightly modified version: int main() { auto l = [](auto...){}; void(*p)(int) = l; } In this case, clang still accepts it while GCC rejects it . Is there any reason for which this code should be rejected or is it a bug of the compiler? I'm going to open an issue, but I'd like to know if there exists any proposal that could have been implemented by one of them and not by the other one. This is a known GCC parsing bug ( 64095 , 68071 ): []

Using SFINAE with generic lambdas

谁说胖子不能爱 提交于 2019-12-04 07:56:08
Can generic lambdas take advantage of the "Substitution Failure Is Not An Error" rule ? Example auto gL = [](auto&& func, auto&& param1, auto&&... params) -> enable_if_t< is_integral< std::decay_t<decltype(param1)> >::value> { // ... }; auto gL = [](auto&& func, auto&& param1, auto&&... params) -> enable_if_t< !is_integral< std::decay_t<decltype(param1)> >::value> { // ... }; Are there any workarounds or plans to include this in the language ? Also since generic lambdas are templated function objects under the hood isn't it a bit odd that this can't be done ? Lambdas are function objects under

When use a function template instead of a generic lambda?

↘锁芯ラ 提交于 2019-12-01 16:19:06
I can write a function template: template<typename T> void f1(T parameter) { ... } But in C++14, I can also create a generic lambda: auto f2 = [](auto parameter) { ... }; Within f1 I can refer to T directly. Within f2 , there's no T to refer to, but I can get the same effect using decltype : auto f2 = [](auto parameter) { using T = decltype(param); ... }; An advantage of the generic lambda is that I can perfect-forward it. I can't do that with the function template: template<typename T> void fwdToG(T&& param) { g(std::forward<T>(param)); } fwdToG(f1); // error! fwdToG(f2); // okay Are there

Why generic lambdas are allowed while nested structs with templated methods aren't?

青春壹個敷衍的年華 提交于 2019-12-01 15:35:44
As far as I understand - generic lambdas are transformed into objects of local scope structs with templated operator() . This makes generic lambda very powerful and easy to use tool. On the other hand one can create structs nested into the function, when however the struct has templated member e.g.: #include <iostream> int main() { struct inner { template <class T> void operator()(T &&i) { } }; return 0; } or is templated by itself: int main() { template <class T> struct inner { void operator()(T &&i) { } }; return 0; } compiler seems to have a problem with compiling it: error: invalid

When use a function template instead of a generic lambda?

时光总嘲笑我的痴心妄想 提交于 2019-12-01 15:11:18
问题 I can write a function template: template<typename T> void f1(T parameter) { ... } But in C++14, I can also create a generic lambda: auto f2 = [](auto parameter) { ... }; Within f1 I can refer to T directly. Within f2 , there's no T to refer to, but I can get the same effect using decltype : auto f2 = [](auto parameter) { using T = decltype(param); ... }; An advantage of the generic lambda is that I can perfect-forward it. I can't do that with the function template: template<typename T> void

C++14: Generic lambda with generic std::function as class member

*爱你&永不变心* 提交于 2019-12-01 04:21:47
Consider this pseudo-snippet: class SomeClass { public: SomeClass() { if(true) { fooCall = [](auto a){ cout << a.sayHello(); }; } else { fooCall = [](auto b){ cout << b.sayHello(); }; } } private: template<typename T> std::function<void(T)> fooCall; }; What I want is a class member fooCall which stores a generic lambda, which in turn is assigned in the constructor. The compiler complains that fooCall cannot be a templated data member. Is there any simple solution on how i can store generic lambdas in a class? There is no way you'll be able to choose between two generic lambdas at run-time, as

C++14: Generic lambda with generic std::function as class member

依然范特西╮ 提交于 2019-12-01 01:17:31
问题 Consider this pseudo-snippet: class SomeClass { public: SomeClass() { if(true) { fooCall = [](auto a){ cout << a.sayHello(); }; } else { fooCall = [](auto b){ cout << b.sayHello(); }; } } private: template<typename T> std::function<void(T)> fooCall; }; What I want is a class member fooCall which stores a generic lambda, which in turn is assigned in the constructor. The compiler complains that fooCall cannot be a templated data member. Is there any simple solution on how i can store generic

Enabling `-std=c++14` flag in Code::Blocks

安稳与你 提交于 2019-11-28 19:42:57
I have installed Code::Blocks for Windows and want to compile C++14 code like generic lambdas but the binary version of Code::Blocks that I've installed from codeblocks.org doesn't support the flag -std=c++14 . How do I update the compiler and enable -std=c++14 flag for Code::Blocks? Andreas DM To compile your source code using C++14 in Code::Blocks, you, first of all, need to download and install a compiler that supports C++14 features. Here’s how you can do it on Windows: Download MinGW from here (particular build) or from official site to choose options Extract it to for example: C:\