C++ inline variable
生活随笔
收集整理的這篇文章主要介紹了
C++ inline variable
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
考慮一個程序庫的設計者,發(fā)明了一個Kath類。在Kath.h:
struct Kath {static int const hi; };static成員變量需要在某個編譯單元(以后簡稱為cpp)中定義。在Kath.cpp:
int const Kath::hi = 0;在兩個地方處理同一個事物,這是非常不爽的。特別是對程序庫的設計者,如果希望提供只有頭文件的一套庫(類似STL),就面對更大的麻煩。
有一些workaround技術(shù),解決這個問題。如:?
template< class Dummy > struct Kath_ {static int const hi; };template< class Dummy > int const Kath_<Dummy>::hi = 0;using Kath = Kath_<void>;利用模板的成員必須在頭文件中定義的規(guī)則。還有:
struct Kath {inline int hi() {static int const hi=0;return hi;} };利用內(nèi)聯(lián)函數(shù),只能在頭文件中定義的規(guī)則。或者C++17 的便利語法如下:
struct Kath {static const int hi; };inline int const Kath::hi = 0;?
轉(zhuǎn)載于:https://www.cnblogs.com/thomas76/p/8528225.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的C++ inline variable的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RT-Thread OS的启动流程
- 下一篇: QT中的事件传递顺序小论