2015-12-22 188 views
1

我讀谷歌C++測試框架上Githubhttps://github.com/google/googletest/blob/master/googletest/include/gtest/gtest-param-test.h有沒有更好的方法來寫這個模板?

我發現作者寫了下面的代碼,

template <typename T1, typename T2, typename T3> 
internal::ValueArray3<T1, T2, T3> Values(T1 v1, T2 v2, T3 v3) { 
return internal::ValueArray3<T1, T2, T3>(v1, v2, v3); 
} 

template <typename T1, typename T2, typename T3, typename T4> 
internal::ValueArray4<T1, T2, T3, T4> Values(T1 v1, T2 v2, T3 v3, T4 v4) { 
return internal::ValueArray4<T1, T2, T3, T4>(v1, v2, v3, v4); 
} 
..... 
.... 
... 
.. 
. 
template <typename T1, typename T2, typename T3, typename T4, typename T5, 
typename T6, typename T7, typename T8, typename T9, typename T10, 
typename T11, typename T12, typename T13, typename T14, typename T15, 
typename T16, typename T17, typename T18, typename T19, typename T20, 
typename T21, typename T22, typename T23, typename T24, typename T25, 
typename T26, typename T27, typename T28, typename T29, typename T30, 
typename T31, typename T32, typename T33, typename T34, typename T35, 
typename T36, typename T37, typename T38, typename T39, typename T40, 
typename T41, typename T42, typename T43, typename T44, typename T45, 
typename T46, typename T47, typename T48, typename T49, typename T50> 
internal::ValueArray50<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, 
T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, 
T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, 
T44, T45, T46, T47, T48, T49, T50> Values(T1 v1, T2 v2, T3 v3, T4 v4, 
T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, 
T14 v14, T15 v15, T16 v16, T17 v17, T18 v18, T19 v19, T20 v20, T21 v21, 
T22 v22, T23 v23, T24 v24, T25 v25, T26 v26, T27 v27, T28 v28, T29 v29, 
T30 v30, T31 v31, T32 v32, T33 v33, T34 v34, T35 v35, T36 v36, T37 v37, 
T38 v38, T39 v39, T40 v40, T41 v41, T42 v42, T43 v43, T44 v44, T45 v45, 
T46 v46, T47 v47, T48 v48, T49 v49, T50 v50) { 
return internal::ValueArray50<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, 
    T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, 
    T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, 
    T40, T41, T42, T43, T44, T45, T46, T47, T48, T49, T50>(v1, v2, v3, v4, 
    v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, 
    v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, 
    v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, 
    v48, v49, v50); 
} 

筆者花代碼的數百行重載函數爲了從2到50參數列表。

我的問題是「是否有更好的方式來寫它」?

謝謝。

- 一個C++ Noob。

+0

「作者」似乎是一個代碼生成器,而不是一個人。閱讀文件中的註釋:'這個文件是由命令產生的:pump.py gtest-param-test.h.pump不要用手編輯!!!'。 – PaulMcKenzie

+0

您可以使用可變參數模板來更系統地表達這種情況,但代碼可能希望可供C++ 98用戶使用。 –

+0

我明白了。感謝您指出它是由代碼生成的。 – IceTeaCup

回答

1

這在C++ 98/03中變得相當普遍。

從C++ 11開始,您通常會對可變參數模板做同樣的事情。實際上,至少乍看起來,整個ValueArray可能非常接近C++ 11所謂的tuple

相關問題