1

Topic: To connect a lambda

Hello! Perhaps incorrectly expressed, but an essence such: there is a necessity to use callback', functors for this purpose well approach. But it would be desirable to have possibility to transfer there and lambdas with nonblank []. Specificity is that that is not present std:: function, especially boost'. What variants are? It is desirable is easier

2

Re: To connect a lambda

And the example of that of that would be desirable but not  - it is possible? And that is not clear

3

Re: To connect a lambda

Hello, reversecode, you wrote: R> and the example of that of that would be desirable but not  - it is possible? And that is not clear Hm. Now at all also I will not tell. It is my first lambda of Kolega potters, at it a fat template , he invents any   to reduce the size of the resultant code - whether a joke to tell - everyone  on 1 the code I add , found any arguing (now I will not find, tomorrow I will specify), there a lambda  through the pointer on function, I decided that it likely that is necessary for me. Started to understand -  more difficult empty not , in  write that it is necessary std:: function. Here, somehow so Sorri if it is chaotic, only started to gnaw 11 standard Thought, guess, in what a problem, and I yet did not realize that  Though, I made something similar that at the colleague, only without  inside, my class turned out not sample, on top screwed a template on five lines, added there  in sample parameter which any lambdas eats as well as functors, and now I think that the colleague not absolutely that wants to make something

4

Re: To connect a lambda

Hello, Marty, you wrote: M> Specificity is that that is not present std:: function, especially boost'. What exactly causes a failure from std:: function? What it is impossible? , type traits? The matter is that to screw something the it is possible, but it will not be better std:: function if to cover the most general case.

5

Re: To connect a lambda

https://github.com/jamboree/CxxFunctionBenchmark here the example of various variations, the people with  like was played can there that for itself find

6

Re: To connect a lambda

Hello, Alexander G, you wrote: M>> Specificity is that that is not present std:: function, especially boost'. AG> What exactly causes a failure from std:: function? It is not present in compiler AG> That else it is impossible? , type traits?  like is, as work - I do not know, like at the very least plow. type traits - AG> the matter is that to screw something the it is possible, but it will not be better std:: function if to cover the most general case. Well here children there similar also saw something a month almost; I, as the new person in office generally, and unfamiliar from a C ++ 11 in particular while especially in it I do not climb. But somehow for half-day fastened to the code with a similar functional a lambda, here and I think that they there saw, and the main thing, whether it is necessary

7

Re: To connect a lambda

Hello, reversecode, you wrote: R> https://github.com/jamboree/CxxFunctionBenchmark R> here the example of various variations, the people with  like was played R> can there that for itself find Sps, tomorrow with a fresh mind I will look

8

Re: To connect a lambda

M> Well here children there similar also saw something a month almost; I, as the new person in office generally, and unfamiliar from a C ++ 11 in particular while especially in it I do not climb. But somehow for half-day fastened to the code with a similar functional a lambda, here and I think that they there saw, and the main thing, whether it is necessary to publish urgently this  for  here http://govnokod.ru/cpp

9

Re: To connect a lambda

Hello, reversecode, you wrote: M>> Well here children there similar also saw something a month almost; I, as the new person in office generally, and unfamiliar from a C ++ 11 in particular while especially in it I do not climb. But somehow for half-day fastened to the code with a similar functional a lambda, here and I think that they there saw, and the main thing, whether it is necessary R> to publish urgently this  for  here http://govnokod.ru/cpp It is impossible. Privacy

10

Re: To connect a lambda

Hello, Marty, you wrote: M> Well here children there similar also saw something a month almost; I, as the new person in office generally, and unfamiliar from a C ++ 11 in particular while especially in it I do not climb. But somehow for half-day fastened to the code with a similar functional a lambda, here and I think that they there saw, and the main thing, whether it is necessary Well, whether it is necessary. Cases different happen. If it is not necessary type erasure, it is possible to transfer simply as the deduced sample parameter. If it is not necessary to support any signatures, only for one the task too strongly becomes simpler. If the lambda does not need to be stored but only to lower downwards on a stack through the pointer on a lambda it is possible to simplify. Good implementation, such as in boost:: function, besides support type erasure, with support of all signatures, and storage  in itself, does still the following: 1. Avoids polymorphism on a C ++ classes in favor of polymorphism on pointers on function. That is that on all possible signatures there was no pack RTTI and vtable. 2. Does small value optimization, that is if the lambda small stores it not on a heap, and on value.

11

Re: To connect a lambda

Hello, Alexander G, you wrote: M>> Well here children there similar also saw something a month almost; I, as the new person in office generally, and unfamiliar from a C ++ 11 in particular while especially in it I do not climb. But somehow for half-day fastened to the code with a similar functional a lambda, here and I think that they there saw, and the main thing, whether it is necessary AG> Well, whether it is necessary. Cases different happen. AG> if it is not necessary type erasure, it is possible to transfer simply as the deduced sample parameter. AG> if it is not necessary to support any signatures, only for one the task too strongly becomes simpler. Well here they just also rested against support of any signatures. , what for, truth. AG> if the lambda does not need to be stored but only to lower downwards on a stack through the pointer on a lambda it is possible to simplify. Here , but, most likely,  it is necessary to store AG> Good implementation, such as in boost:: function, besides support type erasure, with support of all signatures, and storage  in itself, does still the following: AG> 1. Avoids polymorphism on a C ++ classes in favor of polymorphism on pointers on function. That is that on all possible signatures there was no pack RTTI and vtable. AG> 2. Does small value optimization, that is if the lambda small stores it not on a heap, and on value. Heaps are not present. By the way

12

Re: To connect a lambda

Hello, Marty, you wrote: M> Heaps are not present. By the way Ogo. Well somehow so it is possible, time new is forbidden, there is the fixed buffer in itself: template <std:: size_t BufferSize, class R, class... Args> class LambdaStorage {public: template <class F> LambdaStorage (F f) {new (data) F (f); call = CallImpl <F>; copy = CopyImpl <F>; del = DelImpl <F>;} LambdaStorage () {call = nullptr; copy = nullptr; del = nullptr;} LambdaStorage (const LambdaStorage& other) {call = other.call; copy = other.copy; del = other.del; copy (data, other.data);} ~LambdaStorage () {if (del! = nullptr) del (data);} LambdaStorage& operator = (const LambdaStorage& other) = delete; R operator () (Args... args) {return call (data, args...);} private: template <class F> static R CallImpl (const char* data, Args... args) {const F* f = reinterpret_cast <const F *> (data); return (*f) (args...);} template <class F> static void CopyImpl (char* dest, const char* src) {const F* f = reinterpret_cast <const F *> (src); new (dest) F (*f);} template <class F> static void DelImpl (char* data) {const F* f = reinterpret_cast <const F *> (data); f-> ~ F ();} R (*call) (const char* data, Args...); void (*copy) (char* dest, const char* src); void (*del) (char* data); char data [BufferSize];}; typedef LambdaStorage <16, int, const char *, int> MyCallable; MyCallable MakeCallable () {int i = 22; return [i] (const char* s, int j) {std:: cout <<s <<'\n '; return (i+j);};} void UseCallable (MyCallable c) {int r = c ("Hello world!", 20); std:: cout <<"Result" <<r <<'\n ';} int main () {MyCallable c = MakeCallable (); UseCallable (c); return 0;} (the decision is subject to optimization if that, is the evident version)

13

Re: To connect a lambda

Hello, Marty, you wrote: AG>> Good implementation, such as in boost:: function, besides support type erasure, with support of all signatures, and storage  in itself, does still the following: AG>> 1. Avoids polymorphism on a C ++ classes in favor of polymorphism on pointers on function. That is that on all possible signatures there was no pack RTTI and vtable. AG>> 2. Does small value optimization, that is if the lambda small stores it not on a heap, and on value. M> heaps are not present. By the way I such did, someone from a forum helped councils and responses, for a subject inconveniently from a pad to search now. The result lies on https://github.com/pmed/fixed_size_function the Size of the fixed buffer under a lambda is set by template parameter, still from 2 to 4 pointers leaves on call function, , copyings and lambda relocation. However if there is no heap easier all the same to use lambdas without state capture, to reduce all to the pointer on function. Made interfaces normally and project, put any void* argument callback functions: using callback = void (*) (void* user_data, float x); void explode (int y, callback cb, void* user_data); struct world {...} my; explode (1000, [] (void* data, float x) {auto my = static_cast <world *> (data);...}, &my);

14

Re: To connect a lambda

Hello, Marty, you wrote: AG>> What exactly causes a failure from std:: function? M> It is not present in the compiler As such probably that lambdas are in the compiler and std:: function are not present??? And generally same a heading file, most likely it is possible to drag it therefrom where it is (from other compiler, from  etc.)

15

Re: To connect a lambda

Hello, anonymouse2, you wrote: AG>>> What exactly causes a failure from std:: function? M>> It is not present in compiler A> As such probably that lambdas are in the compiler and std:: function are not present??? A> And generally same a heading file, most likely it is possible to drag it therefrom where it is (from other compiler, from  etc.) Like tried, did not fly up. It embedded, the child And Keil, was specific since I saw it for the first time in 96 to year, droplets did not change For me was shock that it supports 11  the standard, let and with cut down STL