2

I have something like this:

boost::function<void(void)> redStyle = boost::bind(colorStyle, "red");

boost::function<void(void)> blueBlinkingStyle = boost::bind(animatedStyle, "blue", BLINKING);

Is this the correct way to define a nullStyler:

void nothing(){}
boost::function<void(void)> noStyle = boost::bind(nothing);

I was thinking that I could do it this way instead but empty functions throw:

boost::function<void(void)> noStyle;

The code using the styler function could check for empty instead of using the "Null Object Pattern". Which is better? Having a weird nothing function in my Detail namespace or checking for empty?

Baz
  • 12,713
  • 38
  • 145
  • 268

1 Answers1

3

I would prefer to have a no-op function. It makes the intent clear, and it saves you from checking whether the functor is empty or not (assuming you wouldn't have to chjeck for other reasons).

Note that you don't need to call bind. You can do this:

boost::function<void(void)> noStyle = nothing;

Example:

#include <boost/function.hpp>
#include <iostream>

void hello()
{
  std::cout << "hello" << std::endl;
}

int main()
{
  boost::function<void(void)> f = hello;
  f();
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480