简体   繁体   English

std :: tr1 :: function是否有一个合理的默认值?

[英]Is there a sane default for std::tr1::function?

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. 我意识到我可以创建一个什么都不做的函数,但我正在寻找一些标准方法,不必处理检查以查看函数是否被赋予了null对象模式的值。

void noop() { }

Could you use a boost::optional<std::tr1::function<void()> > ? 你能使用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. 这样它可以设置或不设置,你可以使用if检查来查看功能是否已设置。

在我的项目中,我使用

void noop(...) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM