大括号在 Boost::extension 中如何工作,如何自己制作这样的宏?

发布于 2024-11-28 05:57:34 字数 790 浏览 1 评论 0原文

我看看我们如何使用 Boost::Extension BOOST_EXTENSION_TYPE_MAP_FUNCTION 宏。

例如 this

BOOST_EXTENSION_TYPE_MAP_FUNCTION
{
    std::map<std::string, boost::extensions::factory<service> > &factories(types.get());
    factories["file_service"].set<file_service>();
}

BOOST_EXTENSION_TYPE_MAP_FUNCTION 宏在 扩展名.hpp

我想知道这个宏如何理解大括号中的内容,以及如何将这个宏扩展为诸如“Hello 扩展宏”之类的内容?

I look at how we use Boost::Extension BOOST_EXTENSION_TYPE_MAP_FUNCTION macro.

For example like this:

BOOST_EXTENSION_TYPE_MAP_FUNCTION
{
    std::map<std::string, boost::extensions::factory<service> > &factories(types.get());
    factories["file_service"].set<file_service>();
}

BOOST_EXTENSION_TYPE_MAP_FUNCTION macro is defined in extension.hpp.

I wonder how this macro understands what is in Curly Brackets and how for example expand this macro to something that would cout anything like "Hello extended macro"?

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

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

发布评论

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

评论(1

倚栏听风 2024-12-05 05:57:34

让我将我的评论放入答案中......

宏是对编译器的指令(我在这里使用集体术语)在该位置替换定义为该宏的符号,例如

#define FOO 1

int val = FOO; // at this point, FOO is replaced with 1

(ps请不要在C++)

现在,您的情况发生的是有一组符号(函数的签名)定义为宏,因此所发生的只是编译器将用符号替换宏,最终结果是看起来(大致)像这样:

void boost_extension_exported_type_map_function(boost::extensions::type_map& types)
{
    std::map<std::string, boost::extensions::factory<service> > &factories(types.get());
    factories["file_service"].set<file_service>();
}

正如你所看到的,这是一个简单的 功能。你也可以这样做(但除非你有一个很好的理由,否则不要这样做)

#define BOB void foo(std::string const& bar)

BOB
{
  std::cout << "HEllo: " << bar << std::endl;
}

它只是允许用户为该函数定义自己的实现......大概在其他地方 - 它获取该函数的地址并通过指针...

Let me put my comment into an answer...

A macro is an instruction to the compiler (I use the collective term here) to substitute at that location the symbols defined as that macro, for example

#define FOO 1

int val = FOO; // at this point, FOO is replaced with 1

(p.s. please don't do this in C++)

Now, what is happening in your case is that there is a set of symbols (the signature of a function) defined as a macro, so all that happens is the compiler will substitute the macro with the symbols, and the end result would look (roughly) like this:

void boost_extension_exported_type_map_function(boost::extensions::type_map& types)
{
    std::map<std::string, boost::extensions::factory<service> > &factories(types.get());
    factories["file_service"].set<file_service>();
}

Which as you can see is a simple function. You can do this too (but don't unless you have a very good reason)

#define BOB void foo(std::string const& bar)

BOB
{
  std::cout << "HEllo: " << bar << std::endl;
}

It simply allows a user to define their own implementation for that function... presumably somewhere else - it takes the address of that function and uses it via a pointer...

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