這個問題在這里已經有了答案: 函式呼叫不能與 C 中的候選模板定義(通過參考接收二維陣列的函式)匹配 (1 個答案) 如何將 VLA 傳遞給函式模板? (6 個回答) 2天前關閉。
template <typename T, int n >
int tell_me_size(T (&) [n]){
return n;
}
此代碼適用于
int a[4];
cout<< tell_me_size(a)<<endl;
而不適用于
int n;
cin>>n;
int a[n];
cout<< tell_me_size(a)<<endl;
后者給出錯誤“沒有匹配函式呼叫'tell_me_size(int [n])”
uj5u.com熱心網友回復:
根據 C 20 標準(13.4.3 模板非型別引數)
2 非型別模板引數的模板引數應是模板引數型別的轉換常量運算式(7.7)。
注意像這樣的可變長度陣列
int n;
cin>>n;
int a[n];
不是標準的 C 功能。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/486484.html
