I have the following code:
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;
}
Which when compiled on MSVC9 SP1 results in tons of C2780 compiler errors. Here are just a few from the top of the list:
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: 'boost::_bi::bind_t<Rt2,boost::_mfi::cmf8<R,T,B1,B2,B3,B4,B5,B6,B7,B8>,_bi::list_av_9<A1,A2,A3,A4,A5,A6,A7,A8,A9>::type> boost::bind(boost::type<T>,R (__thiscall T::* )(B1,B2,B3,B4,B5,B6,B7,B8) const,A1,A2,A3,A4,A5,A6,A7,A8,A9)' : expects 11 arguments - 3 provided
1> c:\code\work\cmake-mds\build-vc9\third_party\boost\1.48.0\include\boost\bind\bind_mf2_cc.hpp(223) : see declaration of 'boost::bind'
1>c:\code\work\cmake-mds\server\gmmserver\domino\server\interface\dimime.cpp(210) : error C2780: 'boost::_bi::bind_t<Rt2,boost::_mfi::mf8<R,T,B1,B2,B3,B4,B5,B6,B7,B8>,_bi::list_av_9<A1,A2,A3,A4,A5,A6,A7,A8,A9>::type> boost::bind(boost::type<T>,R (__thiscall T::* )(B1,B2,B3,B4,B5,B6,B7,B8),A1,A2,A3,A4,A5,A6,A7,A8,A9)' : expects 11 arguments - 3 provided
1> c:\code\work\cmake-mds\build-vc9\third_party\boost\1.48.0\include\boost\bind\bind_mf2_cc.hpp(212) : see declaration of 'boost::bind'
Any compiler errors relating to boost are virtually unreadable and unhelpful to me, so I hope someone can help me figure out what is going on. Thanks in advance.
There are four overloads of
std::string::find:Therefore one must help the compiler to pick one (resolve ambiguity) by specifying which particular overloaded function the address is taken of, e.g.:
Rather ugly, isn’t it?
Note, that
std::string::find()returnsstd::string::npos(which most often issize_t(-1)) on an unsuccessful search. Then it would convertsize_t(-1)tobool(true)and result instd::find_if()returning its first argument no matter what the rest of arguments are.The result of
std::string::find()needs to be compared againststd::string::npos. Usingboost::bindthat would look like:Which doesn’t look too readable either.
It may be a tiny little bit more readable with
boost::lambda::bind:It looks most readable and elegant with C++11 lambda:
Further I noticed that
idreturned for unsuccessful search is 0. It is the same value that is returned when the search succeeds on the first element. In other words, the caller of this function won’t be able to distinguish between an unsuccessful search and when the first (0th) element matched.It is most simple and portable to use a plain loop for search here:
Using this version the caller can easily tell between a successful and unsuccessful search and find out the matching index if necessary:
So, pick your poison…