久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何強制初始化靜態成員?

How to force a static member to be initialized?(如何強制初始化靜態成員?)
本文介紹了如何強制初始化靜態成員?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

考慮這個示例代碼:

template<class D>
char register_(){
    return D::get_dummy(); // static function
}

template<class D>
struct Foo{
    static char const dummy;
};

template<class D>
char const Foo<D>::dummy = register_<D>();

struct Bar
    : Foo<Bar>
{
    static char const get_dummy() { return 42; }
};

(也在 Ideone 上.)

我希望 dummy 在有 Foo 的具體實例化后立即被初始化,我有 Bar.這個問題(以及最后的標準引用)解釋得很清楚,為什么沒有發生.

I'd expect dummy to get initialized as soon as there is a concrete instantiation of Foo, which I have with Bar. This question (and the standard quote at the end) explained pretty clear, why that's not happening.

[...] 特別是,靜態數據成員的初始化(和任何相關的副作用)不會發生,除非靜態數據成員本身以需要靜態數據成員定義的方式使用存在.

[...] in particular, the initialization (and any associated side-effects) of a static data member does not occur unless the static data member is itself used in a way that requires the definition of the static data member to exist.

有什么方法可以強制 dummy 被初始化(有效地調用register_)沒有任何實例BarFoo(沒有實例,所以沒有構造器技巧)并且 Foo 的用戶不需要以某種方式顯式聲明成員?不需要派生類做任何事情的額外 cookie.

Is there any way to force dummy to be initialized (effectively calling register_) without any instance of Bar or Foo (no instances, so no constructor trickery) and without the user of Foo needing to explicitly state the member in some way? Extra cookies for not needing the derived class to do anything.

編輯:找到一種對派生類影響最小的方法:>

struct Bar
    : Foo<Bar>
{   //                              vvvvvvvvvvvv
    static char const get_dummy() { (void)dummy; return 42; }
};

盡管如此,我仍然希望派生類不必這樣做.:|

Though, I'd still like the derived class not having to do that. :|

推薦答案

考慮:

template<typename T, T> struct value { };

template<typename T>
struct HasStatics {
  static int a; // we force this to be initialized
  typedef value<int&, a> value_user;
};

template<typename T>
int HasStatics<T>::a = /* whatever side-effect you want */ 0;

也可以不引入任何成員:

It's also possible without introducing any member:

template<typename T, T> struct var { enum { value }; };
typedef char user;

template<typename T>
struct HasStatics {
  static int a; // we force this to be initialized
  static int b; // and this

  // hope you like the syntax!
  user :var<int&, a>::value,
       :var<int&, b>::value;
};

template<typename T>
int HasStatics<T>::a = /* whatever side-effect you want */ 0;

template<typename T>
int HasStatics<T>::b = /* whatever side-effect you want */ 0;

這篇關于如何強制初始化靜態成員?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 成人黄色在线 | 免费视频一区二区 | 手机看片1024日韩 | 久久久久久久九九九九 | 教室脔到她哭h粗话h好爽视频 | 亚洲一区免费 | 日本少妇高潮达到高潮 | 中文在线字幕观看 | 不卡免费视频 | 亚洲国产中文字幕 | 丁香激情五月 | 欧美日韩a | 欧美黑粗大 | 久久精品视频网站 | 天天干天天干天天 | 毛片网站在线观看 | 午夜视频免费在线观看 | 午夜av在线播放 | 亚洲精品福利视频 | 国产视频福利 | 欧美在线免费观看视频 | 中文字幕亚洲欧美 | 黄色大片免费观看 | 黄色免费观看视频 | 亚洲精品日韩丝袜精品 | 天天操天天插 | 成人综合婷婷国产精品久久 | 特级黄色大片 | 天天色影院 | 日韩在线视频一区 | 色一区二区三区 | 久久精品伊人 | 国产视频在线观看视频 | 欧美成人激情 | 黄色一级网站 | 亚洲精品在线视频 | aaaaaabbbbbb毛片| 亚洲少妇一区 | 国产乱码精品一区二区三 | 欧美日韩一二三 | 成人免费在线播放 |