开发者

Is there a sane default for std::tr1::function?

开发者 https://www.devze.com 2023-01-02 00:37 出处:网络
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);

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.


void noop() { }


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.


In my project I use

void noop(...) {}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号