使用变量#define

发布于 2024-12-16 13:17:09 字数 887 浏览 0 评论 0原文

Libcurl 使用以下内容来定义电子邮件收件人:

#define RECIPIENT "<[email protected]>"

但是如果我不想对收件人进行硬编码怎么办?我希望用户能够提供他/她自己的电子邮件地址,因此我需要找到一种方法来执行此操作:

std::string emailreceiver = "[email protected]";
#define RECIPIENT = emailreceiver

收件人在这一行中使用:

rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);

我假设我不能简单地将其更改为

std::string emailreceiver = "[email protected]";
rcpt_list = curl_slist_append(rcpt_list, emailreceiver);

Anybody has any建议?

Libcurl uses the following to define the email recipient:

#define RECIPIENT "<[email protected]>"

But what if I don't want to hard code the recipient? I want a user to be able to supply his/her own email address, so I need to find a way to do this:

std::string emailreceiver = "[email protected]";
#define RECIPIENT = emailreceiver

The recipient is used in this line:

rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);

I'm assuming I can't simply change this to

std::string emailreceiver = "[email protected]";
rcpt_list = curl_slist_append(rcpt_list, emailreceiver);

Anyone have any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

友谊不毕业 2024-12-23 13:17:10

Curl 需要 C 字符串 (const char *),而不是 C++ 字符串 (std::string)。所以尝试一下:

std::string emailreceiver = "[email protected]";
rcpt_list = curl_slist_append(rcpt_list, emailreceiver.c_str());

根本不需要使用 #define,这只是一个示例。

Curl expects a C string (const char *), not a C++ string (std::string). So try:

std::string emailreceiver = "[email protected]";
rcpt_list = curl_slist_append(rcpt_list, emailreceiver.c_str());

There's no need to use a #define at all, that was just an example.

沫离伤花 2024-12-23 13:17:10

你的最后一个片段可能非常接近。从表面上看,curl 需要一个 C 风格的字符串,因此您可能必须将其更改为:

std::string emailreceiver = "[email protected]";
rcpt_list = curl_slist_append(rcpt_list, emailreceiver.c_str());

Your last snippet is probably pretty close. From the looks of things, curl is expecting a C-style string though, so you may have to change it to:

std::string emailreceiver = "[email protected]";
rcpt_list = curl_slist_append(rcpt_list, emailreceiver.c_str());
以往的大感动 2024-12-23 13:17:10

libcurl 并没有真正做到这一点。您问题中的 #define 与 docs/examples/smtp-multi.c 中的一行最相似:

#define RECIPIENT "<[email protected]>"

该宏仅使用一次,稍后在同一源文件中使用:

rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);

(引用的行来自curl 版本7.23.0。)

正如文件名所暗示的,这只是一个示例。在实际应用程序中,您不太可能希望对收件人名称使用硬连线宏。

curl.hcurl_slist_append 的声明为:(

CURL_EXTERN struct curl_slist *curl_slist_append(struct curl_slist *,
                                                 const char *);

不必担心 CURL_EXTERNconst 现在。)

因此,当您调用 curl_slist_append 时,第二个参数必须是 char*。特别是,它可以是字符串文字,可以直接写入调用中,也可以由宏扩展产生。但它可以是 char* 类型的任何表达式,只要它指向有效的字符串即可。

您需要决定如何确定收件人电子邮件地址,并将指向该字符串的指针(C 样式字符串,而不是 C++ std::string)作为第二个参数传递给 <代码>curl_slist_append。为此目的使用宏可能没有意义。这只是示例程序演示正在发生的事情的一种简单方法。

至于你在评论中的问题:“我仍然好奇是否可以将变量分配给#define。” ——嗯,是的,也不是。您不分配任何东西给#define。 #define(宏定义)是一种编译时构造,它会导致任何出现的宏名称被宏定义的文字文本替换。例如,这个:

#define RECIPIENT "<[email protected]>"
rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);

与这个: 完全相同

rcpt_list = curl_slist_append(rcpt_list, "<[email protected]>");

(除了后者没有定义 RECIPIENT)。如果您将宏定义从 "<[email protected]>"您的任何其他内容就像,那么每次出现的 RECIPIENT 都将被您在 #define RECIPIENT 之后编写的内容替换。

所以你可以做这样的事情:

char *recipient = get_recipient();
#define RECIPIENT recipient
rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);

但这没有真正的意义;你也可以这样写:

rcpt_list = curl_slist_append(rcpt_list, recipient);

预处理器(编译器中处理 #define 指令和宏扩展等的部分)完全不知道函数调用、变量和类似的结构。它只是进行文本替换,而不考虑文本的含义。 (它实际上是根据令牌来定义的。)

这意味着您可以滥用预处理器来做一些危险的事情。 这是一个示例。

libcurl doesn't really do that. The #define in your question most closely resembles a line in docs/examples/smtp-multi.c:

#define RECIPIENT "<[email protected]>"

The macro is used exactly once, later in the same source file:

rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);

(Quoted lines are from curl version 7.23.0.)

As the file name implies, that's only an example. In a real application, it's unlikely that you'd want to use a hardwired macro for a recipient name.

The declaration of curl_slist_append, in curl.h, is:

CURL_EXTERN struct curl_slist *curl_slist_append(struct curl_slist *,
                                                 const char *);

(Don't worry about the CURL_EXTERN or the const for now.)

So when you call curl_slist_append, the second argument has to be a char*. In particular, it can be a string literal, either written directly in the call or resulting from a macro expansion. But it can be any expression of type char*, as long as it points to a valid string.

You need decide how you want to determine the recipient e-mail address, and pass a pointer to that string (a C-style string, not a C++ std::string) as the second argument to curl_slist_append. It probably doesn't make sense to use a macro for that purpose. It was just a simple way for the example program to demonstrate what's going on.

As for your question in a comment: "I'm still curious on if it's possible to assign a variable to a #define or not." -- well, yes and no. You don't assign anything to a #define. A #define (macro definition) is a compile-time construct that causes any occurrence of the macro name to be replaced by the literal text of the macro definition. For example, this:

#define RECIPIENT "<[email protected]>"
rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);

is exactly equivalent to this:

rcpt_list = curl_slist_append(rcpt_list, "<[email protected]>");

(except that the latter doesn't leave RECIPIENT defined). If you change the macro definition from "<[email protected]>" to anything else you like, then each occurrence of RECIPIENT will be replaced by whatever you wrote after #define RECIPIENT.

So you could do something like this:

char *recipient = get_recipient();
#define RECIPIENT recipient
rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);

but there's no real point; you might as well just write:

rcpt_list = curl_slist_append(rcpt_list, recipient);

The preprocessor (the piece of the compiler that handles #define directives and macro expansions, among other things) has absolutely no clue about function calls, variables, and similar constructs. It just does textual replacement without regard to what the text means. (It's actually defined in terms of tokens.)

This means that you can abuse the preprocessor to do some dangerous things. Here's an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文