我有这样的事情:
boost::function<void(void)> redStyle = boost::bind(colorStyle, "red");
boost::function<void(void)> blueBlinkingStyle = boost::bind(animatedStyle, "blue", BLINKING);
这是定义nullStyler的正确方法:
void nothing(){}
boost::function<void(void)> noStyle = boost::bind(nothing);
我以为我可以这样做而不是空函数抛出:
boost::function<void(void)> noStyle;
使用样式器函数的代码可以检查空而不是使用&#34;空对象模式&#34;。哪个更好?在我的Detail命名空间中有一个奇怪的无效函数或检查空?
答案 0 :(得分:3)
我更喜欢没有操作功能。它使意图变得清晰,并且它使您无需检查仿函数是否为空(假设您不必因其他原因而使用chjeck)。
请注意,您无需调用bind。你可以这样做:
boost::function<void(void)> noStyle = nothing;
示例:
#include <boost/function.hpp>
#include <iostream>
void hello()
{
std::cout << "hello" << std::endl;
}
int main()
{
boost::function<void(void)> f = hello;
f();
}