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