2014-12-22 63 views
0

我最近在閱讀gflags的源代碼。這裏的評論讓我很困惑。在gflags源代碼中使用FLAGS_nono ## name

這就是說引入FLAGS_nono ## name來確保靜態初始化。但據我所知,如果你定義一個全局變量如: 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 @[email protected]::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類。 我想知道這裏的類和全局變量的必要性,而不是簡單地定義一個函數。

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 
} 

回答

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 @[email protected]::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 @[email protected]::FlagRegisterer o_##name(\ 
     #name, #type, MAYBE_STRIPPED_HELP(help), __FILE__,    \ 
     &FLAGS_##name, &FLAGS_no##name);         \ 
    }                  \ 
    using fL##shorttype::FLAGS_##name 

但現在你已經有了動態初始化,在那裏你想要的靜態初始化。

爲了確保兩者都正常工作,您需要一個額外的輔助變量,這就是您所要求的代碼。

相關問題