大括号在 Boost::extension 中如何工作,如何自己制作这样的宏?
我看看我们如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我将我的评论放入答案中......
宏是对编译器的指令(我在这里使用集体术语)在该位置替换定义为该宏的符号,例如
(ps请不要在C++)
现在,您的情况发生的是有一组符号(函数的签名)定义为宏,因此所发生的只是编译器将用符号替换宏,最终结果是看起来(大致)像这样:
正如你所看到的,这是一个简单的 功能。你也可以这样做(但除非你有一个很好的理由,否则不要这样做)
它只是允许用户为该函数定义自己的实现......大概在其他地方 - 它获取该函数的地址并通过指针...
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
(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:
Which as you can see is a simple function. You can do this too (but don't unless you have a very good reason)
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...