tags:

views:

72

answers:

2

I spent some time googling but didn't really find anything. I want to be able to do this:

std::tr1::function<void()> foo(SOME_DEFAULT_FUNCTION_THAT_DOES_NOTHING);
//
//Some code that could possibly assign foo
//
foo();

Otherwise I have to do this:

std::tr1::function<void()> foo;
//
//Some code that could possibly assign foo
//
if(foo)
{
    foo();
}

I realize I could just make a function that does nothing, but I am looking for some standard way of not having to deal with checking to see if the function was given a value ala the null object pattern.

A: 

Could you use a boost::optional<std::tr1::function<void()> >? That way it allows it to be set or not, and you can use an if check to see if the function has been set.

Mark B
I can already do an if check. I wanted to avoid the if check and be able to execute the function in either case.
messenger
+3  A: 

void noop() { }

Dennis Zickefoose
Figured as much. Sounds good to me!
messenger