Using lambda or functor as match_condition with boost::asio::read_until()
"Klebsch, Mario via Boost-users" <[email protected]> Tue, 28 Oct 2025 14:30:52 +0000
| Newsgroups | gmane.comp.lib.boost.user |
|---|---|
| Message-ID | <[email protected]> |
Hello,
I want to use boost::asio::read_until() and pass a functor as match conditi=
on. But I don't get it to compile.
Here is my source code:
#include <boost/asio.hpp>
using namespace boost::asio;
using iterator =3D buffers_iterator<streambuf::const_buffers_type>;
struct completion_functor
{
std::pair<iterator, bool> operator()(iterator begin, iterator end) {
return {};
}
};
completion_functor functor;
static_assert(is_match_condition<completion_functor>::value, "completion_fu=
nctor must be match condition");
static_assert(is_match_condition<decltype(functor)>::value, "functor must b=
e match condition");
std::pair<iterator, bool> completion_func(iterator begin, iterator end) {
return functor(begin, end);
}
static_assert(is_match_condition<decltype(completion_func)>::value, "comple=
tion_func must be match condition");
auto lambda =3D [](iterator begin, iterator end) -> std::pair<iterator, boo=
l> {
return {};
};
static_assert(is_match_condition<decltype(lambda)>::value, "lambda must be =
match condition");
std::pair<iterator, bool> completion_func2(iterator begin, iterator end) {
return lambda(begin, end);
}
static_assert(is_match_condition<decltype(completion_func2)>::value, "compl=
etion_func2 must be match condition");
int main(int argc, char *argv[])
{
io_context ctx;
ip::tcp::socket s{ctx};
streambuf buffer;
read_until(s, buffer, '\n');
read_until(s, buffer, completion_func);
read_until(s, buffer, completion_func2);
// the following calls do not compile
// read_until(s, buffer, functor);
// read_until(s, buffer, lambda);
}
It seems, that boost::asio::is_match_condition<> does neither accept my lam=
mda-function nor my functor. The only completion handler, that I get accept=
ed, is a classic, free-standing function (and static class member functions=
, too).
But in that function, calling the lambda or the functor does not seem to be=
a problem for the compiler.
The documentation of read_until describes match_condition as "The function =
object to be called to determine whether a match exists."
I would expect lambda functions and functors to be accepted as a function o=
bject.
Do I have an error in the declaration of lambda or completion_functor::oper=
ator()?
Does anybody now, how to get this working?
I tried boost_1_73_0 and boost_1_88_0 with g++ (Gentoo 14.3.1_p20250801 p4=
) 14.3.1 20250801 and clang version 20.1.8
Thanks in advance,
Klebsch Mario
Funktion | R&D
Tel: +49 (0) 531 38 701 718
Raum: Braunschweig, E20
Diese E-Mail und die an sie angeh=E4ngten Dateien sind ausschlie=DFlich f=
=FCr Personen oder Institutionen bestimmt, deren Namen oben aufgef=FChrt si=
nd. Sie k=F6nnen Informationen enthalten, die durch das Berufsgeheimnis ges=
ch=FCtzt sind und deren Weitergabe strengstens untersagt ist. Jede elektron=
ische Nachricht kann ge=E4ndert werden. ACTIA lehnt jede Verantwortung f=FC=
r diese Nachricht ab. Der Inhalt dieser Nachricht stellt keine Verpflichtun=
g seitens unseres Unternehmens dar. Wenn Sie kein Empf=E4nger sind, weisen =
wir Sie darauf hin, dass das Lesen, Vervielf=E4ltigen oder Verteilen streng=
stens untersagt ist. Wir bitten Sie daher, uns umgehend =FCber diesen Brief=
zu informieren und diese Nachricht sowie eventuell beigef=FCgte Unterlagen=
aus Ihrem Postfach zu l=F6schen. Danke.
This e-mail and the files attached to it are intended exclusively for perso=
ns or institutions whose names are listed above. They may contain informati=
on that is protected by professional secrecy and the disclosure of which is=
strictly prohibited. Any electronic message can be modified. ACTIA decline=
s all responsibility for this message. The content of this message does not=
represent a commitment on the part of our company. If you are not a recipi=
ent, we would like to point out that reading, copying or distribution is st=
rictly prohibited. We therefore ask you to inform us immediately about this=
letter and to delete this message and any accompanying documents from your=
mailbox. Thank you.
_______________________________________________
Boost-users mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://lists.boost.org/mailman3/lists/boost-users.lists.boost.org/
Archived at: https://lists.boost.org/archives/list/[email protected].=
org/message/EFS3KB5CF4OT2FHLELO43GRZR4W6RWLY/=20