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?

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Or. std::function<void(void)> noStyle = []{}; ... which requires you to type 3 character less.
@Nawaz good point, although I always assumed one uses boost::function when one doesn't have C++11 support.
@Angew I'm obviously taking the whole "C++11 is C++" thing a bit too far :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.