編輯:讓我們suposse我有兩個(或多個)模板函數f
和使用g
(有時),這取決於它的模板參數類型:模板別名
template<typename T>
some_ugly_and_large_or_deep_template_struct_1<T>::type
f(const some_ugly_and_large_or_deep_template_struct_1<T>::type&,
const some_ugly_and_large_or_deeptemplate_struct_1<T>::type&)
{
// body, that uses perhaps more times my
// "some_ugly_and_large_or_deep_template_struct_1<T>"
}
template<typename T>
some_ugly_and_large_or_deep_template_struct_2<T>::type
g(const some_ugly_and_large_or_deep_template_struct_2<T>::type&,
const some_ugly_and_large_or_deeptemplate_struct_2<T>::type&)
{
// body, that uses perhaps more times my
// "some_ugly_and_large_or_deep_template_struct_2<T>"
}
我怎麼能簡化這種「類型」定義?例如使用任何新的C++ 11工具?我認爲只有在這樣的:
template<typename T,
typename aux = some_ugly_and_large_or_deep_template_struct_1<T>::type>
aux f(const aux&, const aux&)
{
// body, that uses perhaps more times my
// "aux" type
}
template<typename T,
typename aux = some_ugly_and_large_or_deep_template_struct_2<T>::type>
aux g(const aux&, const aux&)
{
// body, that uses perhaps more times my
// "aux" type
}
,我用這種方法看到的問題是,用戶可以提供自己的aux
類型,而不是我想要的類型。
爲了確保類型是你願意,你可以使用'static_assert'與'的std :: is_same'什麼。 – hmjd
@hmjd但是我應該'寫'兩次包含'some_ugly_and_large_or_deep_template_struct :: type'的行,並且我可以有許多具有相同問題的功能(並且還降低了可讀性)。 –
與宏? :#define DEEP_TYPE(T)some_ugly_and_large_or_deep_template_struct :: type –
Francois