[問題] template class 內 static 初始問題
https://wandbox.org/permlink/skxmougiYnaw1f1a
一開始遭遇到這個compile warning
warning: instantiation of variable 'Foo<int>::bar' required here, but no defin
ition is available [-Wundefined-var-template]
其實我不太知道他為什麼會warning
我確實有define在test.cpp這個translation unit
不懂的是 他感覺找不到定義 卻讓我pass, run time 也有拿到對的數值.
而g++不會有warning
1. 到這邊我還是不知道到底哪裡寫的不夠正確?
然而我試著解決這warning
於是我把 上面的
//b.
// extern template class Foo<int>;
打開
https://wandbox.org/permlink/j2ANWRBoeIZZJAHE
link error
undefined reference to `Foo<int>::Test()'
如果不呼叫 這個warning確實可以這樣解決
但我必須呼叫這function
2. 為什麼他這樣會說undefined? .h裡面明確有包含定義阿?
然而
//a.
// template class Foo<int>;
打開後
3. 我認為我已經明確讓他產生程式碼了.... warning還是存在
回歸1. 的問題 我到底少做了什麼讓clang這樣出warning
4. 對於template class內 有static function or data
最正確的寫法該怎麼寫.
網路上有查到
在test.h 直接寫
template<class T>
int Foo<T>::bar = 初始直;
test.cpp一樣寫
template<>
int Foo<int>::bar = 123;
但我實際上在專案遇到一個況狀是
我某個cpp 寫Foo<int>::bar 拿到的卻是.h給的初始直(我認為是他初始化順序優先於te
st.cpp)
所以目前毫無辦法處理這warning
5. 這似乎沒辦法用static是internal linkage 來解釋...讓我整個無法通透理解
請教各位有什麼方法處理這問題
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 27.242.196.52
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1529658222.A.8D0.html
→
06/22 18:21,
7年前
, 1F
06/22 18:21, 1F
→
06/22 18:23,
7年前
, 2F
06/22 18:23, 2F
→
06/22 18:24,
7年前
, 3F
06/22 18:24, 3F
※ 編輯: lovejomi (27.242.196.52), 06/22/2018 18:27:10
推
06/23 00:19,
7年前
, 4F
06/23 00:19, 4F
→
06/23 00:19,
7年前
, 5F
06/23 00:19, 5F
→
06/23 02:02,
7年前
, 6F
06/23 02:02, 6F
→
06/23 02:02,
7年前
, 7F
06/23 02:02, 7F
→
06/23 02:02,
7年前
, 8F
06/23 02:02, 8F
推
06/23 02:07,
7年前
, 9F
06/23 02:07, 9F
→
06/23 02:08,
7年前
, 10F
06/23 02:08, 10F
→
06/23 02:08,
7年前
, 11F
06/23 02:08, 11F
→
06/23 14:48,
7年前
, 12F
06/23 14:48, 12F
→
06/23 14:48,
7年前
, 13F
06/23 14:48, 13F
推
06/23 23:08,
7年前
, 14F
06/23 23:08, 14F
→
06/24 16:03,
7年前
, 15F
06/24 16:03, 15F
→
06/24 22:50,
7年前
, 16F
06/24 22:50, 16F
→
06/24 22:50,
7年前
, 17F
06/24 22:50, 17F
→
06/25 10:59,
7年前
, 18F
06/25 10:59, 18F
→
06/25 10:59,
7年前
, 19F
06/25 10:59, 19F
→
06/25 10:59,
7年前
, 20F
06/25 10:59, 20F
→
06/25 11:00,
7年前
, 21F
06/25 11:00, 21F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章