我想問一下為什么這不會將資料重新定位到所需的部分,
template <typename T>
struct Retram {
static T data;
inline auto operator=(const T& other) {
data = other;
return *this;
}
operator auto &() const {
return data;
}
operator auto *() const {
return &data;
}
};
template <typename T>
__attribute__((section(".retram"))) T Retram<T>::data = T();
當這樣做時
struct Retram {
static int data;
inline auto operator=(const int& other) {
data = other;
return *this;
}
operator auto &() const {
return data;
}
operator auto *() const {
return &data;
}
};
__attribute__((section(".retram"))) int Retram::data = int();
歡迎您提供替代的漂亮解決方案,但我仍然想了解為什么這不起作用。
兩者都可以編譯,但模板版本不會根據需要重新定位符號。
最小的可重現示例:
Retram<int> my_retram_value;
my_retram_value = 42;
printf("retram value: %i\n", *my_retram_value);
uj5u.com熱心網友回復:
Gcc 通常會忽略模板中的屬性。多年來,這是一個已知的錯誤。有超過 10 個與此問題相關的未解決的錯誤報告。見bugzilla。該問題不僅影響成員函式,還影響變數模板和自由模板函式。
有人已經開始研究它,但在遇到麻煩后-flto他放棄了。補丁評論目前似乎沒有人對修復這個非常古老的問題感興趣。
尤其是對于嵌入式設備,這是一個真正的表演障礙。這使得無法將模板化資料放置到閃存中,或者無法在模板化代碼中運行中斷處理程式等等。
在其中一個錯誤上再次運行的人應該在錯誤報告中對“已知失敗”和“最后確認”欄位進行評論。也許這可以看出問題仍然存在,并且修復對更多人來說很有趣。但與開源專案一樣:產品通過幫助他們變得更好。我沒有 gcc 開發經驗,抱歉 :-)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/529531.html
標籤:C 模板
