加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – boost :: fusion :: for_each中的函数对象与std :: for_eac

发布时间:2020-12-16 06:57:29 所属栏目:百科 来源:网络整理
导读:在升级到更新的编译器并解决编译器错误时,我意识到boost :: fusion :: for_each要求传入的函数对象具有运算符const. Boost的示例: struct increment{ templatetypename T void operator()(T t) const { ++t; }};...vectorint,int vec(1,2);for_each(vec,inc
在升级到更新的编译器并解决编译器错误时,我意识到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?我显然无法改变这一点,但我想了解为什么这两者有所不同.

感谢您的任何见解和解释!

解决方法

可能需要constness来防止仿函数的内部状态变化,因为,没有为每个元素按顺序定义operator()调用的顺序.因此,随之而来的呼叫不应该相互依赖.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读