r/cpp • • Jan 30 '23

Web Scraping with C++

https://www.scrapingbee.com/blog/web-scraping-c++/#building-the-web-scraper
245 Upvotes

44 comments sorted by

View all comments

4

u/daedalususedperl Jan 30 '23

I'm confused about this line, what does it do?

typedef size_t( * curl_write)(char * , size_t, size_t, std::string * );

16

u/nysra Jan 30 '23

It's a type alias. It makes curl_write the type of a pointer to functions that return size_t and takes arguments char*, size_t, size_t, std::string*. However it is terrible code because it's C code, in actual C++ you'd at the very least be using using and never typedef but ideally properly put that entire thing into a concept and use templates.

5

u/[deleted] Jan 30 '23

Luckily the curl api has a void* data param, so we can actually wrap any callable into a type that exposes a std::function like interface with two methods, one to get the callback that calls the callable and one to get the volid *. The really nasty ones are the ones without that data param

1

u/joaquintides Boost author Jan 31 '23

How to do the trick is described here (and surely in many other places):

https://bannalia.blogspot.com/2016/07/passing-capturing-c-lambda-functions-as.html

1

u/[deleted] Jan 31 '23

Thanks, nice writeup. I have a struct I wrote that just does it for me, it's common enough. I haven't explorer the case of doing actual thunking when there is no void*data pointer.

1

u/joaquintides Boost author Jan 31 '23 edited Jan 31 '23

Thunking can’t be done portably alas. Good candidate for a Boost library I’d say.

1

u/[deleted] Jan 31 '23

Both of these are hard to do, yeah. Well this one isn't but too distracting from the code at hand. Not sure if a full library that does things to make C-apis less errorprone, or a core thing.