boost::fusion::for_each 中的函数对象与 std::for_each 不同
在升级到较新的编译器并解决编译器错误时,我意识到 boost::fusion::for_each 要求传入的函数对象具有运算符 const。
来自 Boost 的示例:
struct increment
{
template<typename T>
void operator()(T& t) const
{
++t;
}
};
...
vector<int,int> vec(1,2);
for_each(vec, increment());
这当然没有改变。我没有意识到它与 std::for_each
,它不要求运算符是 const。
struct increment
{
template<typename T>
void operator()(T& t) // no const here!!!
{
++t;
}
};
std::vector<int> numbers;
std::for_each(numbers.begin(), numbers.end(), increment());
是否有任何明显的理由需要 const
?我显然无法改变这一点,但我想了解为什么这两者不同。
感谢您的任何见解和解释!
While upgrading to a newer compiler and resolving compiler errors I realized that boost::fusion::for_each
requires that the function object passed in has the operator const
.
Example from Boost:
struct increment
{
template<typename T>
void operator()(T& t) const
{
++t;
}
};
...
vector<int,int> vec(1,2);
for_each(vec, increment());
This has of course not changed. I didn't realized it's different to std::for_each
, which does not require the operator to be const
.
struct increment
{
template<typename T>
void operator()(T& t) // no const here!!!
{
++t;
}
};
std::vector<int> numbers;
std::for_each(numbers.begin(), numbers.end(), increment());
Is there any obvious reason for requiring const
? I obviously cannot change that, but I'd like to understand why these two differ.
Thanks for any insights and explanations!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能需要常量来防止仿函数的内部状态更改,因为没有按顺序为每个元素定义operator() 调用的顺序。因此,后续的调用不应相互依赖。
Constness may be required to prevent internal state changes of the functor, since, the order of the operator() calls is not defined for each element in sequence. So, the consequent calls should not depend on each other.