2

I was reading the caffe source codes. In the caffe.cpp which is the source of tools/caffe, I encounter the following codes which puzzles me:

#define RegisterBrewFunction(func) \
namespace { \
class __Registerer_##func { \
 public: /* NOLINT */ \
  __Registerer_##func() { \
    g_brew_map[#func] = &func; \
  } \
}; \
__Registerer_##func g_registerer_##func; \
}

Based on my knowledge, this macro replace RegisterBrewFunction(func) with a anonymous class, and the only thing it has done is add <#func, &func> to g_brew_map. So why not just doing this like this?

#define RegisterBrewFunction(func) g_brew_map[#func]=&func;

Hope someone help me about this.

4

2 回答 2

0

#define定义了一个可以在函数内部使用的宏。当代码到达这个函数时,宏参数将被注册。您必须调用该函数来冲泡咖啡。

原始#define定义了一个应在文件范围内使用的宏。它创建了一个或多或少的匿名对象。该对象的类的构造函数将在main调用之前运行并注册该函数。不需要其他功能。

于 2014-12-29T14:40:09.110 回答
0

因为赋值可能不在全局范围内,只能在函数内部。

这个宏可以用在更多的地方,而且通常会在一些源文件的顶部,这样你就不必调用一个函数来使它工作。

于 2014-12-29T17:37:47.777 回答