2017-07-18 27 views
1

理論上,編譯時應該知道類型,編譯器也知道維數。目前,我有一個模板函數,它將矩陣的維度作爲模板參數。我可以通過估算constexpr中的尺寸或通過模板函數來避免這種情況嗎?估計函數參數數組的維數

struct cont {}; 
void ffd<3>::run(cont mat[3][3][3]) 

在理解中,我想避免聲明rows參數。

template<uint8_t rows> 
struct ffd { 
    template<class T> 
    static float run(const T &mat) { 
     // recursion over the rows in mat 
    } 
}; 
+0

所以,你想從'T'得到'rows'在編譯時? –

+0

我的第一個想法也是不應該的。但類型在編譯時已知,不是嗎? – dgrat

+1

我可以看到3個模板參數的實例,另外還有3個維度的數量。這個void'ffd <4> :: run'的參數是什麼? 'void ffd <4> :: run(float mat [4] [4] [4])','void ffd <4> :: run(float mat [3] [3] [3] [3])'or still something else ?目前你的問題是*不清楚* ... –

回答

3

什麼要搜索是std::extent

template< class T, unsigned N = 0> 
struct extent; 

其中

如果T是一個數組類型,提供了構件恆定值等於 數如果N在 [0,s TD ::秩::值)


例如,對於

float a[10][11][12]; 

時調用run(a)

template<class T> 
float run(const T &mat) 
{ 
    std::extent<T, 0>::value; // 10 
    std::extent<T, 1>::value; // 11 
    std::extent<T, 2>::value; // 12 

} 
+0

你知道它是如何工作的另一個函數參數? http://cpp.sh/46lbb – dgrat

+1

@dgrat你有與你在前面的例子相同的問題。這裏數組衰減指針發生在'proxy'的參數上.' proxy'的實際參數類型是'int(*)[5] [6]'。 – bolov