我可以将 libcurls CURLOPT_WRITEFUNCTION 与 C++11 lambda 表达式一起使用吗?
我尝试将 C++11 lambda 表达式与 CURLOPT_WRITEFUNCTION 结合使用,但程序在运行时因访问冲突而崩溃。由于缺乏 C++11 知识,我不确定如何进一步研究这个问题,但也许其他人知道如何实现这项工作。
功能:
#ifndef CURL_GET_H
#define CURL_GET_H
#include <curl/curl.h>
#include <curl/easy.h>
#include <vector>
#include <string>
std::vector<std::string> curl_get(const char* url)
{
CURL *curl;
CURLcode res;
std::vector<std::string> content;
auto curl_callback = [](void *ptr, size_t size, size_t nmemb, void *stream) -> size_t {
// does nothing at the moment due to testing...
return size * nmemb;
};
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/aaa.txt");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return content;
}
#endif // CURL_GET_H
错误:
lpip_dl.exe 中的 Unbehandelte Ausnahme bei 0x000000cc:0xC0000005:Zugriffsverletzung bei 位置 0x00000000000000cc。
(位置 0x00000000000000cc 处发生访问冲突)
当curl想要使用回调时发生:
wrote = data->set.fwrite_func(ptr, 1, len, data->set.out);
I tried to use a C++11 lambda expression with CURLOPT_WRITEFUNCTION, but the program crashes at runtime with an access violation. I'm not sure how to look further into this due to lack of C++11 knowledge, but maybe someone else has an idea how to make this work.
The function:
#ifndef CURL_GET_H
#define CURL_GET_H
#include <curl/curl.h>
#include <curl/easy.h>
#include <vector>
#include <string>
std::vector<std::string> curl_get(const char* url)
{
CURL *curl;
CURLcode res;
std::vector<std::string> content;
auto curl_callback = [](void *ptr, size_t size, size_t nmemb, void *stream) -> size_t {
// does nothing at the moment due to testing...
return size * nmemb;
};
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/aaa.txt");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return content;
}
#endif // CURL_GET_H
The error:
Unbehandelte Ausnahme bei 0x000000cc in lpip_dl.exe: 0xC0000005: Zugriffsverletzung bei Position 0x00000000000000cc.
(Access violation at position 0x00000000000000cc)
Happens when curl wants to use the callback:
wrote = data->set.fwrite_func(ptr, 1, len, data->set.out);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上,您可以通过将 lambda 函数转换为函数指针来做到这一点。您可以首先创建一个 typedef 以使强制转换更容易。
然后你使用static_cast。
注意:为了转换为C函数指针,只能使用空的captures[]。
You actually can do that by casting the lambda function to function pointer. You can first make a typedef to make cast easier.
Then you use static_cast.
Note: In order to convert to C function pointer, you can only use empty captures [].
这可以通过 + 前缀来完成,它返回一个 C 风格的函数指针。但这仅适用于无状态 lambda(空捕获列表,即 [])。
或者
This can be done with the + prefix, which returns a C-style function pointer instead. But this only works with stateless lambdas (empty capture list i.e. []).
or
libcurl 是纯 C 库,您需要设置一个可以从此类库调用的回调。这意味着有趣的 C++ 东西需要首先“C'ified”才能工作。就像旧式的函数指针一样。
libcurl 常见问题解答条目“使用 C++ 非静态函数进行回调?”
另请参阅:C++11 中的 C 风格回调
libcurl is plain C library, you need to set a callback that can be called from a such. This means funny C++ things need to be "C'ified" first to work. Like into an old-style function pointer.
This is also addressed in the libcurl FAQ entry "Using C++ non-static functions for callbacks?"
See also: C-style Callbacks in C++11
在 Linux 中,将 lambda 转换为正确的函数指针对我来说很有效:
Casting the lambda to the proper function pointer works for me in Linux: