generic-lambda

return type deduction of recursive function

微笑、不失礼 提交于 2021-02-07 06:52:17
问题 Recently, I read Barry's answer to this question Recursive lambda functions in C++11: template <class F> struct y_combinator { F f; // the lambda will be stored here // a forwarding operator(): template <class... Args> decltype(auto) operator()(Args&&... args) const { // we pass ourselves to f, then the arguments. // [edit: Barry] pass in std::ref(*this) instead of *this return f(std::ref(*this), std::forward<Args>(args)...); } }; // deduction guide template <class F> y_combinator(F) -> y

Async void lambda expressions

吃可爱长大的小学妹 提交于 2021-01-24 08:06:24
问题 A quick google search will tell you to avoid using async void myMethod() methods when possible. And in many cases there are ways to make it possible. My question is basically an offshoot of this best practice: What does the lambda expression below evaluate to? Task.Run( async ()=> await Task.Delay(1000)); If it becomes an async Task then we are following best practice. But what if it evaluates to async void ? 回答1: The documentation for expression lambdas says, An expression lambda returns the

How to Write a Lambda Wrapping a Function with Optional Return Value

我只是一个虾纸丫 提交于 2021-01-05 11:46:45
问题 I have tried to write a lambda that measures the execution time of arbitrary functions. With a lot of help I have managed that for C++14 and functions having a return value, see Measure execution time of arbitrary functions with C++14 lambda. Then I wanted my code to also work with C++11, therefore I have implemented the same idea with template functions. Finally I have realized that this code does not work for functions having no return value. It has been quite simple to generalize the

Is it legal to explicitly specify a generic lambda's operator() template arguments?

亡梦爱人 提交于 2020-12-08 05:51:00
问题 Is the following C++ code standard compliant? #include <iostream> int main() { [](auto v){ std::cout << v << std::endl; }.operator()<int>(42); } Both clang++ 3.8.0 and g++ 7.2.0 compile this code fine (the compiler flags are -std=c++14 -Wall -Wextra -Werror -pedantic-errors ). 回答1: This is indeed standard compliant. The standard specifies there must be a member operator() , and that it has one template argument for every occurence of auto in its paramater-declaration-clause. There is no

What is the need of template lambda introduced in C++20 when C++14 already has generic lambda?

泄露秘密 提交于 2020-11-30 04:22:42
问题 c++14 introduced generic lambdas that made it possible to write following: auto func = [](auto a, auto b){ return a + b; }; auto Foo = func(2, 5); auto Bar = func("hello", "world"); It is very clear that this generic lambda func works just like a templated function func would work. Why did the C++ committee decide to add template syntax for generic lamda? 回答1: C++14 generic lambdas are a very cool way to generate a functor with an operator () that looks like this: template <class T, class U>

What is the need of template lambda introduced in C++20 when C++14 already has generic lambda?

送分小仙女□ 提交于 2020-11-30 04:21:10
问题 c++14 introduced generic lambdas that made it possible to write following: auto func = [](auto a, auto b){ return a + b; }; auto Foo = func(2, 5); auto Bar = func("hello", "world"); It is very clear that this generic lambda func works just like a templated function func would work. Why did the C++ committee decide to add template syntax for generic lamda? 回答1: C++14 generic lambdas are a very cool way to generate a functor with an operator () that looks like this: template <class T, class U>

returning LinkedHashMap from IntStream, Java 8

◇◆丶佛笑我妖孽 提交于 2020-05-21 01:57:06
问题 I have this code. private static Map<Long, List<TimePitchValue>> alternativeMethod(AudioFormat audioformat, List<ChunkDTO> listChunkDTO, long index, int sizeChunk) { int numBytesPerSample = audioformat.getSampleSizeInBits() / 8; int quantitySamples = sizeChunk / numBytesPerSample; long baseTime = quantitySamples * index; Map<Long, List<TimePitchValue>> mapListTimePitchValue = new LinkedHashMap<>(); for (int i = 0; i < quantitySamples; i++) { int time = i; List<TimePitchValue>

returning LinkedHashMap from IntStream, Java 8

南笙酒味 提交于 2020-05-21 01:56:13
问题 I have this code. private static Map<Long, List<TimePitchValue>> alternativeMethod(AudioFormat audioformat, List<ChunkDTO> listChunkDTO, long index, int sizeChunk) { int numBytesPerSample = audioformat.getSampleSizeInBits() / 8; int quantitySamples = sizeChunk / numBytesPerSample; long baseTime = quantitySamples * index; Map<Long, List<TimePitchValue>> mapListTimePitchValue = new LinkedHashMap<>(); for (int i = 0; i < quantitySamples; i++) { int time = i; List<TimePitchValue>

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

∥☆過路亽.° 提交于 2020-01-30 04:04:31
问题 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

Should non-capturing generic lambdas decay to function pointers?

江枫思渺然 提交于 2019-12-22 05:03:03
问题 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