首页 > 解决方案 > 为什么 stl 算法按值获取可调用对象?

问题描述

我在尝试类似的东西时偶然发现了这一点

#include <vector>
#include <algorithm>

struct pred { //not copyable
    pred () {}
    bool operator()(int) const { return false; }
    pred( pred && ) {}
};

int main () {
    std::vector<int> a{1,2,3};
    std::vector<int> b{};
    auto p = pred{};
    //std::copy_if(a.begin(), a.end(), std::back_inserter(b), p); // this fails
    std::copy_if(a.begin(), a.end(), std::back_inserter(b), std::move(p));
}

https://godbolt.org/z/wDvO3K

为什么 stl 算法不能接受“操作”的通用引用?

PS:抱歉,如果这是重复的,如果不是,我会感到惊讶..

标签: c++pass-by-valuestl-algorithm

解决方案


推荐阅读