1

Topic: How to create such ljambda - function?

2

Re: How to create such ljambda - function?

Decided that my only moved object is very similar on unique_ptr and for check wrote such code:

void OpenFile (std::unique_ptr<std::ifstream>&& File, cstring& FileName)
{
File-> open (FileName, std:: ios:: in);
}
int main (int argc, char *argv [])
{
std:: unique_ptr <std:: ifstream> File (new std:: ifstream);
std:: function <void (void)> Bind5 = [File = std:: move (File)] () mutable///an error: use of deleted function
{
std:: string FileName;
OpenFile (std:: move (File), FileName);
};
}

But with shares_ptr the same code perfectly works:

void OpenFile (std::shared_ptr<std::ifstream>&& File, cstring& FileName)
{
File-> open (FileName, std:: ios:: in);
}
int main (int argc, char *argv [])
{
std:: shared_ptr <std:: ifstream> File (new std:: ifstream);
std:: function <void (void)> Bind5 = [File = std:: move (File)] () mutable
{
std:: string FileName;
OpenFile (std:: move (File), FileName);
};
}

In other words the question can be paraphrased so: how in lambdas to use only moved objects, for example, unique_ptr?

3

Re: How to create such ljambda - function?

While decided to use the ugly decision and instead of:

class MyClass
{
public:
MyClass () {}
MyClass (const MyClass& Value) = delete;//IT IS DELETED
MyClass (MyClass&& Value) {}
};

To write:

class MyClass
{
public:
MyClass () {}
MyClass (const MyClass& Value) {throw std:: logic_error ("Use COPY - constuctor of Smart array is denied!");}
MyClass (MyClass&& Value) {}
};

So in compile time the designer of copying turns out accessible, and in runtime at its usage the error gets out.