gpt4 book ai didi

c++ - std::bind 与 std::less_equal

转载 作者:太空宇宙 更新时间:2023-11-04 15:24:33 26 4
gpt4 key购买 nike

我尝试编译这个函数:

#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>

void foo(unsigned n)
{
std::vector<unsigned> some_vector;
/* fill vector ... */
auto le_sqrt_n = std::bind(std::less_equal<unsigned>,
std::placeholders::_1,
unsigned(sqrt(n))
);
auto it = std::find_if(some_vector.rbegin(), some_vector.rend(), le_sqrt_n);
/* do something with the result ... */
}

使用 gcc 4.6.3:

g++ -std=c++0x test_bind_02.cc

并得到这个错误:

test_bind_02.cc: In function ‘void foo(unsigned int)’:
test_bind_02.cc:10:55: error: expected primary-expression before ‘,’ token
test_bind_02.cc:13:9: error: unable to deduce ‘auto’ from ‘<expression error>’
test_bind_02.cc:14:77: error: unable to deduce ‘auto’ from ‘<expression error>’

我的(大概是愚蠢的)错误是什么?

最佳答案

你不调用std::less_equal<unsigned> c-tor .此代码工作正常。

http://liveworkspace.org/code/7f779d3fd6d521e8d4012a4066f2c40f

std::bind有两种形式。

template< class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );
template< class R, class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );

std::less_equal是你应该传递给这个函数的结构 F完全构造的对象。工程代码为

#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>

void foo(unsigned n)
{
std::vector<unsigned> some_vector;
/* fill vector */
auto le_sqrt_n = std::bind(std::less_equal<unsigned>(),
std::placeholders::_1,
unsigned(sqrt(n))
);
auto it = std::find_if(some_vector.rbegin(), some_vector.rend(), le_sqrt_n);
}

关于c++ - std::bind 与 std::less_equal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11948934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com