我有一种情况,即boost :: function和boost :: bind(实际上是std :: tr1 :: function和bind)在被使用时被删除.这样安全吗?我通常会避免它,但有问题的代码有点根深蒂固,我唯一的另一个选择是添加一个新线程.
typedef function<int(int)> foo_type;
foo_type* global_foo = NULL;
int actual_foo( int i, Magic* m )
{
delete global_foo;
return m->magic(i);
}
int main()
{
Magic m;
global_foo = new foo_type( bind( &actual_foo, _1, &m );
return (*global_foo)(10)
}
Run Code Online (Sandbox Code Playgroud)
绑定参数始终是普通的整数类型(实际代码中的int和指针),而不是引用.
我试图从矢量中删除短字符串.
std::vector<std::string> vec;
// ...
vec.erase(std::remove_if(vec.begin(),
vec.end(),
boost::bind(std::less<size_t>(),
boost::bind(&std::string::length, _1),
5),
vec.end());
Run Code Online (Sandbox Code Playgroud)
编译器会发出一个非常大的错误消息:
qwer.cpp:20: error: no matching function for call to 'remove_if(__gnu_cxx::__nor
mal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char
> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator
<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::al
locator<char> > > > >, __gnu_cxx::__normal_iterator<std::basic_string<char, std:
:char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_strin
g<char, std::char_traits<char>, std::allocator<char> > > > >, boost::_bi::bind_t
<boost::_bi::unspecified, std::less<unsigned int>, boost::_bi::list2<boost::_bi:
:bind_t<unsigned int, boost::_mfi::cmf0<unsigned int, std::basic_string<char, st
d::char_traits<char>, std::allocator<char> > >, boost::_bi::list1<boost::arg<1>
> >, boost::_bi::value<int> > >, __gnu_cxx::__normal_iterator<std::basic_string<
char, …Run Code Online (Sandbox Code Playgroud) 在谈话期间boost::bind,有人注意到它std::bind1st存在于C++ 03中,但它"几乎无法使用".
我找不到任何可靠的支持.
boost :: bind是标准函数std :: bind1st和 std :: bind2nd的泛化.它支持任意函数对象,函数,函数指针和成员函数指针,并且能够将任何参数绑定到特定值或将输入参数路由到任意位置.bind不对函数对象提出任何要求; 特别是,它不需要 result_type,first_argument_type和 second_argument_type标准typedef.
或许暗示这些限制确实适用于std::bind1st.
比对参数的数目明显的限制,还有什么是优势boost::bind,以std::bind1st/ std::bind2nd?std::bind1st在C++ 03中"几乎无法使用" 的断言是否有任何优点?
我试图使用boost::bind和STL boost::tuple,但每次我尝试编译我得到以下错误.
error: call of overloaded ‘bind(<unresolved overloaded function type>,
boost::arg<1>&)’ is ambiguous
Run Code Online (Sandbox Code Playgroud)
你知道我在这里做错了什么,为什么只是为了boost::arg<1>?
谢谢AFG
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <boost/tuple/tuple.hpp>
#include <boost/assign.hpp>
#include <boost/bind.hpp>
int main( int argc, const char** argv ){
using namespace boost::assign;
typedef boost::tuple< int, double > eth_array;
std::vector< eth_array > v;
v+= boost::make_tuple( 10,23.4), boost::make_tuple( 12,24.4) );
std::for_each( v.begin()
, v.end()
, boost::bind<int>(
printf
, "%d-%f"
, boost::bind( eth_array::get<0>, _1 )
, boost::bind( eth_array::get<1>, _1 ) …Run Code Online (Sandbox Code Playgroud) Compiler: g++ 4.4.3 Boost...: 1.49.0 OS......: Ubuntu
注意:自从我认真使用C++已有15年了,所以我正在重新学习和学习新东西,因为我也尝试学习Boost.
给出以下代码:
1. class Beta {
2. public:
3. std::string name();
4. }
5.
6. class Alpha {
7. public:
8. Beta m_beta;
9. }
10.
11. Alpha one;
Run Code Online (Sandbox Code Playgroud)
由于各种原因,我想使用boost:bind来实现与调用"one.m_beta.name()"相同的结果.我认为以下会这样做:
12. boost::function<std::string(Alpha)>
13. b = boost::bind(
14. &Beta::name,
15. boost::bind(&Alpha::m_beta, _1)
16. );
17. cout << "NAME = " << b(one) << "\n";
Run Code Online (Sandbox Code Playgroud)
但是当我编译(在Ubuntu上的g ++ 4.4.3)时,我收到以下错误:
错误:从'const Beta*'无效转换为'Beta*'
在查看由第13-16行产生的实际类型定义之后,看起来第15行变为'const Beta*',但是包装它的绑定期望'Beta*'.
但是,这个DOES工作:
30. boost::function<std::string(Beta)>
31. p1 = boost::bind(&Beta::name,_1);
32. boost::function<Beta(Alpha)>
33. p2 = boost::bind(&Alpha::m_beta,_1); …Run Code Online (Sandbox Code Playgroud) 有时我倾向于编写仿函数,而不是为了在函数调用之间维护状态,而是因为我想捕获函数调用之间共享的一些参数.举个例子:
class SuperComplexAlgorithm
{
public:
SuperComplexAlgorithm( unsigned int x, unsigned int y, unsigned int z )
: x_( x ), y_( y ), z_( z )
{}
unsigned int operator()( unsigned int arg ) const /* yes, const! */
{
return x_ * arg * arg + y_ * arg + z_;
}
private:
// Lots of parameters are stored as member variables.
unsigned int x_, y_, z_;
};
// At the call site:
SuperComplexAlgorithm a( 3, 4, 5 ); …Run Code Online (Sandbox Code Playgroud) 假设我有class A一个公共方法void f(int sig).在A我的构造函数中添加了
signal(SIGSEV, boost::bind(&A::f, this, _1));
Run Code Online (Sandbox Code Playgroud)
这将返回编译错误
error : cannot convert `boost::_bi::bind_t<void, boost::_mfi::mf1<void, A, int>, boost::_bi::list2<boost::_bi::value<A*>, boost::arg<1> > >' to `__sighandler_t {aka void (*)(int)}' for argument `2' to `void (* signal(int, __sighandler_t))(int)'
Run Code Online (Sandbox Code Playgroud)
知道为什么吗?
为什么回调只调用一次?
bool callback()
{
static bool res = false;
res = !res;
return res;
}
int main(int argc, char* argv[])
{
vector<int> x(10);
bool result=false;
for_each(x.begin(),x.end(),var(result)=var(result)||bind(callback));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这有什么问题:
template <typename T>
std::list<T> & operator+=(std::list<T> & first, std::list<T> const& second)
{
std::for_each(second.begin(), second.end(), boost::bind(&std::list<T>::push_back, first, _1));
return first;
}
Run Code Online (Sandbox Code Playgroud)
编译很好,但不起作用.
我有以下代码:
int MimeDocument::GetAttachmentId( std::string const& content_id )
{
using namespace boost::lambda;
using boost::lambda::_1;
using boost::bind;
int id = 0;
std::vector<std::string>::iterator it =
std::find_if( attachment_list_.begin(), attachment_list_.end(),
bind( &std::string::find, content_id, _1 ) != std::string::npos
);
if( it != attachment_list_.end() ) {
id = std::distance( attachment_list_.begin(), it );
}
return id;
}
Run Code Online (Sandbox Code Playgroud)
在MSVC9 SP1上编译时会导致大量C2780编译器错误.以下是列表顶部的一些内容:
1>c:\code\work\cmake-mds\server\gmmserver\domino\server\interface\dimime.cpp(210) : error C2780: 'boost::_bi::bind_t<_bi::dm_result<MT::* ,A1>::type,boost::_mfi::dm<M,T>,_bi::list_av_1<A1>::type> boost::bind(M T::* ,A1)' : expects 2 arguments - 3 provided
1> c:\code\work\cmake-mds\build-vc9\third_party\boost\1.48.0\include\boost\bind\bind.hpp(1728) : see declaration of 'boost::bind'
1>c:\code\work\cmake-mds\server\gmmserver\domino\server\interface\dimime.cpp(210) : error C2780: …Run Code Online (Sandbox Code Playgroud) boost-bind ×10
c++ ×10
boost ×3
stl ×3
c++11 ×2
boost-lambda ×1
boost-tuples ×1
signals ×1
std ×1