1

I have a class

class MyClass
{
   typedef std::function<void(const std::string &str> SomeFunc;
   SomeFunc Func;
}

I want to have set for this function. I did one

void MyClass::SetFunc(SomeFunc Func1)
{
    Func = Func1;
}

I need in my class funtion as member and I want to use std::function, because it's alowes to use lambda. If the lambda is complicated, I will need move of function. If lambda is simply, a copy will be nice. And i don't know how to do it

Olich
  • 31
  • 3
  • 3
    moving a pointer *is* a copy. The question is rather unclear. Do you actually want `SoemFunc Func;` as member? – 463035818_is_not_an_ai Sep 08 '21 at 14:52
  • 1
    If you don't want to *move*, why are you calling `move`? What's wrong with `Func = Func1;`? – NathanOliver Sep 08 '21 at 14:57
  • 2
    Sounds like you are confusing function pointers and `std::function`. Just use `SomeFunc` wherever you currently use `SomeFunc*` and it should be much easier. – François Andrieux Sep 08 '21 at 14:58
  • after the edit I dont see the issue with the code. "How I can do move? I need 2 set: copy and move." do you mean you need to overloads of `SetFunc`, one that copies and one that moves? Did you try to write a second overload? I wouldnt bother too much about moving vs copying a `std::function` though – 463035818_is_not_an_ai Sep 08 '21 at 15:03
  • Yes, I mean this question @463035818_is_not_a_number – Olich Sep 08 '21 at 15:16
  • if you post a [mcve] including the real class definition and a lambda for which you need to move the question would be much easier to answer – 463035818_is_not_an_ai Sep 08 '21 at 15:18

1 Answers1

2

I find the answer Should I copy an std::function or can I always take a reference to it?

void MyClass::SetFunc(const SomeFunc &Func1)
{
    Func= Func;
}

void MyClass::SetFunc(SomeFunc &&Func1)
{
    Func= std::move(Func);
}
Olich
  • 31
  • 3