1

Topic: Not std:: function and std:: bind

2

Re: Not std:: function and std:: bind

AlekseySQL;
Likely bind is not able to transfer the rv-reference && as is, and somewhere inside replaces them on and or generally with object copies.
Replace && on and, and std:: move in line with bind on std:: ref. (And for Data too it is necessary std:: ref, by the way, or to remove the link from argument type)
Type of it:

std:: bind (&Write<Type, NeedLock>, std::>>> ref <<<(foo),>>> std:: ref <<<(Data))

And if you have to move foo that  shared_ptr
PS. I think as with rv-references it is possible to be perverted and invent as, but what for...

3

Re: Not std:: function and std:: bind

Anatoly Moskovsky wrote:

AlekseySQL;
Likely bind is not able to transfer the rv-reference && as is, and somewhere inside replaces them on and or generally with object copies.
Replace && on and, and std:: move in line with bind on std:: ref. (And for Data too it is necessary std:: ref, by the way, or to remove the link from argument type)
Type of it:

std:: bind (&Write<Type, NeedLock>, std::>>> ref <<<(foo),>>> std:: ref <<<(Data))

And if you have to move foo that  shared_ptr
PS. I think as with rv-references it is possible to be perverted and invent as, but what for...

the Documentation tells another:

wrote:

Arguments for binding will be copied or moved, and never transferred under the link if only are not wrapped in std:: ref or std:: cref.

Besides I already have a working code in which bind accepts moved unique_ptr (I it enveloped iostream that at origin of an exception the file was correctly closed).

4

Re: Not std:: function and std:: bind

AlekseySQL wrote:

it is passed...
the Documentation tells another:
it is passed...
Besides I already have a working code in which bind accepts moved unique_ptr (I it enveloped iostream that at origin of an exception the file was correctly closed).

bind  it can be caused more once. Therefore transmission to it rv argument is senseless. Therefore it them also does not take. You can construct bind  with rv argument but cannot execute it because of described in the first sentence.
In your "operating" code there is no execution bind  with rv argument.

5

Re: Not std:: function and std:: bind

Anatoly Moskovsky;
Unfortunately, to use references it does not turn out: I have an iteration on a cycle to repeated filling / clearing of an auxiliary array which through  is transferred for asynchronous disk writing. If I transfer the link to an array wrapper passing to records of the new client I I will clear an array and I will fill with its new data, i.e. under this link other records will already lie...
So it is necessary or to transfer on value (that is very bad by operation with arrays), or to move...

6

Re: Not std:: function and std:: bind

OoCc wrote:

bind  it can be caused more once. Therefore transmission to it rv argument is senseless. Therefore it them also does not take. You can construct bind  with rv argument but cannot execute it because of described in the first sentence.
In your "operating" code there is no execution bind  with rv argument.

You are probably right. Glanced in the "operating" code:

void AsyncExecuteDeleteProcedureOfUniquePtr (const variativeData& Settings;
uptr_Type&& File)
{
std:: function <void (void) >&& Bind = std:: bind (File.get_deleter (), File.release ());
...
}

In other words from unique_ptr I pulled out the crude pointer and function of removal of object and them saved in . So to say, "" unique_ptr for its location in .
Though this approach seems to me wrong: the programmer for this purpose also is necessary to track logic of operation and if there will be an error of repeated relocation of object in such errors it and receives the bread for trials.

7

Re: Not std:: function and std:: bind

More shortly, I was tangled in two trees: it is necessary to move at first the array to function (accepting NOT under the right link), and at creation bind to push or on value (as my object stores the pointer on the selected storage and weighs very little), or to use cref. I simplified the previous variant, removing templates:

#include <functional>
#include <iostream>
namespace
{
class MyClass
{
int* p;
public:
//disable copying and assignment
MyClass ():
p (nullptr)
{}
MyClass (const MyClass& Value) = delete;
MyClass& operator = (const MyClass& Value) = delete;
~MyClass () = default;
MyClass (MyClass&& Value):
p (Value.p)
{
Value.p = nullptr;
}
MyClass& operator = (MyClass&& Value) noexcept
{
p = Value.p;
Value.p = nullptr;
return *this;
}
};
void Write (const MyClass& foo)//accepted foo under the link
{
std:: cout <<"Hello, world!" <<std:: endl;
}
void BigWrite (MyClass foo)//here foo contains  the pointer
{
std:: function <void (void)> Bind = std:: bind (&Write, std:: cref (foo));
Bind ();
}
}
int main (int argc, char *argv [])
{
MyClass foo;
BigWrite (std:: move (foo));//now here foo empty and it it is possible to fill with the data again
}

8

Re: Not std:: function and std:: bind

I will continue the dialogue smile
In the last example the logical error is admitted: when I quit function BigWrite the local variable foo is destroyed and therefore in bind -  the link to garbage lies. Certainly, in the resulted example bind to an output from function causes the operator () and errors like as are not present, but if it is used for asynchronous performance of actions (and to be put in any collection for the subsequent performance) the similar approach does not work. Therefore in bind it is necessary to put value, instead of the link (knowingly developers in bind all accept on value).

9

Re: Not std:: function and std:: bind

10

Re: Not std:: function and std:: bind

Clarified that a problem in absence of the designer of copying smile
It is necessary so:

MyClass (const MyClass <Type, NeedLock>& Value) = default;

But me all   doubts: really in any way it is impossible in bind to move object? Really mandatory to create a copy?

11

Re: Not std:: function and std:: bind

AlekseySQL wrote:

But me all   doubts: really in any way it is impossible in bind to move object? Really mandatory to create a copy?

Since a C ++ 14 it is possible.
But not through bind, and through a lambda.

std:: function <void (void)> Bind = [foo {std:: move (foo)}] () {
Write (std:: move (foo));
};

The received object will be movable
Google "Generalized lambda capture".
PS. In a C ++ 11 too it is possible to be perverted in most cases. For this purpose it is necessary to write a wrapper which implements semantics of relocation in the designer of copying. But here certainly it is necessary accurately))

12

Re: Not std:: function and std:: bind

Anatoly Moskovsky, thanks!

13

Re: Not std:: function and std:: bind

OoCc wrote:

bind  it can be caused more once. Therefore transmission to it rv argument is senseless. Therefore it them also does not take. You can construct bind  with rv argument but cannot execute it because of described in the first sentence

At Myers's Cattle ("Effective and modern With ++") on 232 page it in an example moves the data in bind, and in the last paragraph is said that thus in bind forms lvalue - a copy of the moved object which can be used repeatedly. More shortly, I and did not understand why everywhere is written that in bind it is possible to move, and on the fact - an error.

14

Re: Not std:: function and std:: bind

AlekseySQL;
And still Mejers advises not to use bind', preferring lambdas.

15

Re: Not std:: function and std:: bind

AlekseySQL wrote:

in the last paragraph it is said that thus in bind forms lvalue - a copy of the moved object which can be used repeatedly. More shortly, I and did not understand why everywhere is written that in bind it is possible to move, and on the fact - an error.

lvalue - The copy is possible only if at you copying)) is authorized

16

Re: Not std:: function and std:: bind

More precisely bind you make, but copy it somewhere cannot.
But to cause without copying can.

17

Re: Not std:: function and std:: bind

Anatoly Moskovsky wrote:

you are more exact bind make, but copy it somewhere cannot.
But to cause without copying can.

Thanks, for the help in searches. But an error  on a creation line bind. In subsequent (more low on the code) I  with the help std:: move move this to queue for asynchronous performance by a separate flow (I do not copy, namely I move). So like your thought wide of the mark.

18

Re: Not std:: function and std:: bind

AlekseySQL;
Because here copying:

std:: function <void (void)> Bind = std:: bind...

And here that is not present:

auto Bind = std:: function <void (void)> Bind = std:: bind

19

Re: Not std:: function and std:: bind

was.
And here is not present:

auto Bind = Bind = std:: bind...

20

Re: Not std:: function and std:: bind

The last attempt
And here is not present:

auto Bind = std:: bind...

21

Re: Not std:: function and std:: bind

Anatoly Moskovsky, and it is possible more in detail, why here
There is a copying:

std:: function <void (void)> Bind = std:: bind...

, And here is not present???:

auto Bind = std:: bind...

If the qualifier auto could comprise a link or temporary exile sign I would understand, but after all it contains only type... Completely not clearly.

22

Re: Not std:: function and std:: bind

Decided to clarify what functions are caused and wrote:

namespace
{
class MyClass
{
int* p;
public:
//disable copying and assignment
MyClass ():
p (nullptr)
{
std:: cout <<"Create" <<std:: endl;
}
MyClass (const MyClass& Value)
{
std:: cout <<"Copy" <<std:: endl;
}
MyClass& operator = (const MyClass& Value)
{
std:: cout <<"Assignment" <<std:: endl;
}
~MyClass ()
{
std:: cout <<"Destructor" <<std:: endl;
}
MyClass (MyClass&& Value):
p (Value.p)
{
Value.p = nullptr;
std:: cout <<"Moving copy" <<std:: endl;
}
MyClass& operator = (MyClass&& Value) noexcept
{
p = Value.p;
Value.p = nullptr;
std:: cout <<"Moving assignment" <<std:: endl;
return *this;
}
};
void Write (const MyClass& foo)
{
std:: cout <<"Hello, world!" <<std:: endl;
}
void BigWrite (const MyClass& foo)//IN COMMENTS SPECIFIED THE OUTPUT
{
auto Bind = std:: bind (&Write, foo);//Copy
auto Bind2 = std:: bind (&Write, std:: move (foo));//Copy
std:: function <void (void)> Bind3 = std:: bind (&Write, foo);//Copy, Moving copy, Destructor
std:: function <void (void)> Bind4 = std:: bind (&Write, std:: move (foo));//Copy, Moving copy, Destructor
Bind ();//Hello, world!
}//+Destructor on everyone created Bind
}
int main (int argc, char *argv [])
{
MyClass foo;//Create
BigWrite (foo);
return 1;
}

Why the qualifier auto so changes a state of affairs?

23

Re: Not std:: function and std:: bind

Decided to continue experiments and received even more  an output:

void BigWrite (const MyClass& foo)
{
auto&& Bind = std:: move (std:: bind (&Write, foo));//Copy, Destructor
auto Bind2 = std:: move (std:: bind (&Write, foo));//Copy, Moving copy, Destructor
std:: function <void (void)> Bind3 = std:: move (std:: bind (&Write, foo));//Copy, Moving copy, Moving copy, Destructor, Destructor
std:: function <void (void) >&& Bind4 = std:: move (std:: bind (&Write, foo));//Copy, Moving copy, Moving copy, Destructor, Destructor
}

In the first line it is caused  (!!!) and if after this line to write Bind () as Write instead of the filled object foo, we receive empty object... Similar to understand c the strange object bind it does not turn out and it is necessary to leave simply on lambdas...

24

Re: Not std:: function and std:: bind

And here ljambda - functions work well (I have a pool of tasks which have a field std:: function <void (void)>):

std:: cout <<"___________________________________" <<std:: endl;
auto Bind5 = [foo = std:: move (foo)]
{
Write (foo);
};
asyncTask* const CurrentTask5 = new asyncTask (Settings, std:: move (Bind5));
AsyncPool-> AddTask (CurrentTask5);
CurrentTask5-> operator () ();
std:: cout <<"___________________________________" <<std:: endl;
std:: function <void (void)> Bind6 = [foo = std:: move (foo2)]
{
Write (foo);
};
asyncTask* const CurrentTask6 = new asyncTask (Settings, std:: move (Bind6));
AsyncPool-> AddTask (CurrentTask6);
CurrentTask6-> operator () ();
std:: cout <<"___________________________________" <<std:: endl;
___________________________________
Moving copy
Moving copy
Moving copy
Destructor
Hello, world!
___________________________________
Moving copy
Moving copy
Destructor
Hello, world!
___________________________________ 

It is visible that if Bind to declare with the help std:: function <void (void)> the amount of relocation is reduced.