-2

So apparently std::bind takes a bit of processing power/ time to do, so I figure I want to execute any binds at the start of a program, when I'm defining my classes and such, rather than do a lot of them during runtime.

Here's what I WANT to be able to do:

class House {
     public:
       float value;
    
     protected:
       void appreciate;
    }
    

    void House::appreciate(float increasedValue) {
      value += increasedValue;
    }

#include <vector>

class HousesManager {

  public:
  vector<House> houseList;

  protected:
    auto boundHousesAppreciateMethod; // Computationally expensive, so dont want to do it during runtime
  void appreciateTheHouse;
}

HousesManager: HousesManager() {
  boundHousesAppreciateMethod = std::bind<&House::appreciate, _____________>; // Bind during initialization
}
void HousesManager::appreciateTheHouse(House& theHouseInQuestion, float theAmount) {
   theHouseInQuestion.boundHousesAppreciateMethod(theAmount);  // Something like this
   boundHousesAppreciateMethod(theHouseInQuestion, theAmount); // Or Like This
}

Please excuse the contrived nature of this example of Houses.. the point is that I need to be able to call a bound function on a specific instance of a class that's passed in as an parameter to another class method.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
DSiegmeyer
  • 17
  • 3
  • 3
    _"So apparently std::bind takes a bit of processing power/ time to do"_ [citation needed] – YSC Oct 25 '22 at 07:50
  • 1
    Do you even need `std::bind`? Can you just use a member function pointer instead? – Alan Birtles Oct 25 '22 at 07:52
  • 1
    *"`std::bind<&House::appreciate, _____________>`"* -- what exactly is this binding? All I see is function. What *data* is being bound to the function? See [cppreference](https://en.cppreference.com/w/cpp/utility/functional/bind): *"Calling this wrapper is equivalent to invoking `f` with some of its arguments bound to `args`."* If none of the arguments are bound, then the wrapper is wasteful. – JaMiT Oct 25 '22 at 08:07
  • @YSC citation - https://www.youtube.com/watch?v=JtUZmkvroKg 3:30 here – DSiegmeyer Oct 25 '22 at 14:42
  • Kind of wasteful to use `std::bind` without binding anything, but I guess the question comes down to using placeholders for the two unbound parameters. So does this answer your question? [Binding a std::function to a member function in c++?](https://stackoverflow.com/questions/68224037/binding-a-stdfunction-to-a-member-function-in-c) – JaMiT Nov 02 '22 at 07:13
  • If you cannot show how much processing power `bind` is wasting, you don't really care. – n. m. could be an AI Nov 02 '22 at 07:32
  • 1
    Your "citation" talks about compilation time. It has nothing to do with doing things at run time. – n. m. could be an AI Nov 02 '22 at 07:36

1 Answers1

0

I'm not totally sure why do you want to bind it, but I'll show you how:

https://godbolt.org/z/1Y4Pn4869

You can use the type std::function<void()> and then bind to a specific member:

A a;
auto func = std::bind(&A::MyFunc, &a);
B b {func};

I don't like binds, they are usually messy and only should be in core libraries or in lambda implementations.

If you need to replace a behavior I'd recommend using the Strategy pattern.
Combining it with C++ CRTP and Generics yield some powerful compile-time behavior.

struct MyFunc
{
    void operator ()()
    {
        std::cout << "A\n";
    }
};

template <class TFunc>
struct C
{
    void MyOtherFunc()
    {
        TFunc func;
        func();
    }
};

// somewhere else
C<MyFunc> c;
c.MyOtherFunc();

This is a basic example and it shows how types and generics in C++ can be more powerful tools to yield compile time expressions.

SimplyCode
  • 318
  • 2
  • 9