在gflags源代码中使用FLAGS_nono ## name

时间:2014-12-22 08:28:11

标签: c++ gflags

我最近在阅读gflags的源代码。这里的评论让我很困惑。

它引用FLAGS_nono ##名称来确保静态初始化。但据我所知,如果你定义一个全局var,如: int x = 20;

x仍然是静态初始化的。这里有FLAGS_nono ##名称的必要性。

我误解了吗?

感谢。

// Each command-line flag has two variables associated with it: one
// with the current value, and one with the default value.  However,
// we have a third variable, which is where value is assigned; it's a
// constant.  This guarantees that FLAG_##value is initialized at
// static initialization time (e.g. before program-start) rather than
// than global construction time (which is after program-start but
// before main), at least when 'value' is a compile-time constant.  We
// use a small trick for the "default value" variable, and call it
// FLAGS_no<name>.  This serves the second purpose of assuring a
// compile error if someone tries to define a flag named no<name>
// which is illegal (--foo and --nofoo both affect the "foo" flag).
#define DEFINE_VARIABLE(type, shorttype, name, value, help)             \
  namespace fL##shorttype {                                             \
    static const type FLAGS_nono##name = value;                         \
    /* We always want to export defined variables, dll or no */         \
    GFLAGS_DLL_DEFINE_FLAG type FLAGS_##name = FLAGS_nono##name;        \
    type FLAGS_no##name = FLAGS_nono##name;                             \
    static @GFLAGS_NAMESPACE@::FlagRegisterer o_##name( \
      #name, #type, MAYBE_STRIPPED_HELP(help), __FILE__,                \
      &FLAGS_##name, &FLAGS_no##name);                                  \
  }                                                                     \
  using fL##shorttype::FLAGS_##name

有关更多背景信息,请参阅 https://code.google.com/p/gflags/source/browse/src/gflags.h.in#471

另一个问题是上面提到的代码中使用的FlagRegisterer类。 我想知道这样的类和全局var的必要性,而不是简单地定义一个函数。

class GFLAGS_DLL_DECL FlagRegisterer {
 public:
  FlagRegisterer(const char* name, const char* type,
                 const char* help, const char* filename,
                 void* current_storage, void* defvalue_storage);
};


// --------------------------------------------------------------------
// FlagRegisterer
//    This class exists merely to have a global constructor (the
//    kind that runs before main(), that goes an initializes each
//    flag that's been declared.  Note that it's very important we
//    don't have a destructor that deletes flag_, because that would
//    cause us to delete current_storage/defvalue_storage as well,
//    which can cause a crash if anything tries to access the flag
//    values in a global destructor.
// --------------------------------------------------------------------

FlagRegisterer::FlagRegisterer(const char* name, const char* type,
                               const char* help, const char* filename,
                               void* current_storage, void* defvalue_storage) {
  if (help == NULL)
    help = "";
  // FlagValue expects the type-name to not include any namespace
  // components, so we get rid of those, if any.
  if (strchr(type, ':'))
    type = strrchr(type, ':') + 1;
  FlagValue* current = new FlagValue(current_storage, type, false);
  FlagValue* defvalue = new FlagValue(defvalue_storage, type, false);
  // Importantly, flag_ will never be deleted, so storage is always good.
  CommandLineFlag* flag = new CommandLineFlag(name, help, filename,
                                              current, defvalue);
  FlagRegistry::GlobalRegistry()->RegisterFlag(flag);   // default registry
}

1 个答案:

答案 0 :(得分:0)

int i = 10;这样的东西执行静态初始化是对的。因此可以写

#define DEFINE_VARIABLE(type, shorttype, name, value, help)             \
  namespace fL##shorttype {                                             \
    /* We always want to export defined variables, dll or no */         \
    GFLAGS_DLL_DEFINE_FLAG type FLAGS_##name = value;                   \
    type FLAGS_no##name = value;                                        \
    static @GFLAGS_NAMESPACE@::FlagRegisterer o_##name( \
      #name, #type, MAYBE_STRIPPED_HELP(help), __FILE__,                \
      &FLAGS_##name, &FLAGS_no##name);                                  \
  }                                                                     \
  using fL##shorttype::FLAGS_##name

但是,请注意这是一个宏。对value的多次引用会导致多次扩展。如果value 不是常量,如果它包含例如函数调用,这将导致函数被调用两次。

通过将一个变量复制到另一个变量可以避免这种情况:

#define DEFINE_VARIABLE(type, shorttype, name, value, help)             \
  namespace fL##shorttype {                                             \
    /* We always want to export defined variables, dll or no */         \
    GFLAGS_DLL_DEFINE_FLAG type FLAGS_##name = value;                   \
    type FLAGS_no##name = FLAGS_##name;                                 \
    static @GFLAGS_NAMESPACE@::FlagRegisterer o_##name( \
      #name, #type, MAYBE_STRIPPED_HELP(help), __FILE__,                \
      &FLAGS_##name, &FLAGS_no##name);                                  \
  }                                                                     \
  using fL##shorttype::FLAGS_##name

但现在你已经进行了动态初始化,你需要静态初始化。

为确保两者都正常工作,您需要一个额外的辅助变量,这就是您所询问的代码所做的事情。