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

如何強(qiáng)制初始化靜態(tài)成員?

How to force a static member to be initialized?(如何強(qiáng)制初始化靜態(tài)成員?)
本文介紹了如何強(qiáng)制初始化靜態(tài)成員?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

考慮這個(gè)示例代碼:

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 的具體實(shí)例化后立即被初始化,我有 Bar.這個(gè)問(wèn)題(以及最后的標(biāo)準(zhǔn)引用)解釋得很清楚,為什么沒(méi)有發(fā)生.

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.

[...] 特別是,靜態(tài)數(shù)據(jù)成員的初始化(和任何相關(guān)的副作用)不會(huì)發(fā)生,除非靜態(tài)數(shù)據(jù)成員本身以需要靜態(tài)數(shù)據(jù)成員定義的方式使用存在.

[...] 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.

有什么方法可以強(qiáng)制 dummy 被初始化(有效地調(diào)用register_)沒(méi)有任何實(shí)例BarFoo(沒(méi)有實(shí)例,所以沒(méi)有構(gòu)造器技巧)并且 Foo 的用戶不需要以某種方式顯式聲明成員?不需要派生類(lèi)做任何事情的額外 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.

編輯:找到一種對(duì)派生類(lèi)影響最小的方法:>

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

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

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;

這篇關(guān)于如何強(qiáng)制初始化靜態(tài)成員?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡(jiǎn)單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問(wèn)向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對(duì)普通變量進(jìn)行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會(huì)導(dǎo)致訪問(wèn)沖突?)
主站蜘蛛池模板: 2020天天操| 国产精品一码二码三码在线 | 亚洲视频一区二区三区 | 色婷婷一区二区三区四区 | 亚洲精品大片 | 亚洲国产精品久久人人爱 | 国产成人精品网站 | 国产精品久久久久aaaa九色 | 欧洲精品在线观看 | 精品1区| 欧美片网站免费 | 亚洲精品久久久久久首妖 | 综合色久| 91精品一区 | 欧美三区视频 | 国产精品色| 天天躁日日躁性色aⅴ电影 免费在线观看成年人视频 国产欧美精品 | 国产精品美女久久久久久免费 | 涩涩视频在线观看 | 国产视频精品区 | 精品美女视频在线观看免费软件 | 国产激情视频在线 | 国产成人精品免高潮在线观看 | 成人免费在线播放视频 | 午夜av电影院 | 91在线视频免费观看 | 久久黄色 | 黄色一级大片在线免费看产 | 99精品99 | 欧美日韩在线精品 | japan21xxxxhd美女| 欧美精品一区二区三区在线播放 | 天天综合天天 | 56pao在线| 久久a久久| 视频一区二区在线 | 亚洲有码转帖 | 国产精品久久久久久久久久不蜜臀 | 午夜寂寞福利视频 | www久久爱 | 欧美一级大片免费观看 |