我試圖將指針傳遞給作爲模板參數的struct
s數組。我總算過了一段時間做到這一點:作爲模板參數的結構數組
struct something{};
constexpr const something single;
constexpr const something array[12];
template<const something* arg> void f() {}
template<typename T>
constexpr T* workaround(T* v){
return v;
};
void bind(){
f<&single>(); //OK
f<array>(); //NO
f<&array>(); //NO
f<&array[0]>(); //NO
f<workaround(array)>(); //NO
f<(const something*)array>(); //OK
}
這是一個地方的陣列不能透明地用作指針罕見的情況下,或者是一個編譯器錯誤?
你試過指向數組? – Pubby
FWIW,Apple clang v4.1(clang v3.1)不會編譯大部分內容。對於初學者來說,它拒絕'single'和'array'的默認初始化,因爲'something'沒有用戶定義的默認構造函數。當我改變它以便它們被初始化時(例如'constexpr const Elem single {}'),它拒絕調用'f'報告的所有變體:「候選模板被忽略:模板參數'arg'的無效顯式指定參數。 – imaginaryboy