模板函数中的模板别名 [英] Template alias in template function
问题描述
EDITED:让我们同意我有两个(或多个)模板函数 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,也许更多次使用我的
//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,也许更多次使用我的
//some_ugly_and_large_or_deep_template_struct_2< T>
}
如何简化这个类型定义?新的C ++ 11的工具?我认为只有类似:
模板< typename T,
typename aux = some_ugly_and_large_or_deep_template_struct_1< T> :: type> ;
aux f(const aux&); const aux&)
{
// body,可能多次使用我的
//aux类型
}
template< typename T,
typename aux = some_ugly_and_large_or_deep_template_struct_2< T> :: type>
aux g(const aux& const aux&)
{
// body,可能多次使用我的
//aux类型
}
我看到这个方法的问题是用户可以提供自己的 aux
如果你使它成为一个可变参数模板,调用者不可能定义后面列出的类型参数:
模板< typename T,
typename ..., // firewall,吸收所有提供的参数
typename aux = some_ugly_and_large_or_deep_template_struct_1< T> :: type>
aux f(const aux&); const aux&)
{
// body,可能多次使用我的
//aux类型
}
为防止调用 f
许多模板参数,可以添加一个static_assert:
template< typename T,
typename ... F,
typename aux = some_ugly_and_large_or_deep_template_struct_1< T> :: type>
aux f(const aux& const aux&)
{
static_assert(sizeof ...(F)== 0,太多模板参数);
// body,可能多次使用我的
//aux类型
}
通常,我可以让让用户定义类型 aux
,例如返回类型,这可以保存一个转换。 / p>
或者您可以用 enable_if
替换 static_assert
模板< typename T,
typename ... F,typename = typename std :: enable_if< sizeof .. 。(F)== 0> :: type,
typename aux = some_ugly_and_large_or_deep_template_struct< T> :: type,>
aux f(const aux&); const aux&)
{
// body,可能多次使用我的
//aux类型
}
EDITED: Let us suposse I have two (or more) template functions f
and g
that uses (some times) types depending on its template parameter:
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>"
}
How could I simplify this "type" definition?, for example with any of new C++11's tools? I think only on something like:
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
}
The problem that I see with this approach is the user can provide his own aux
type and not the type that I want.
If you make it a variadic template, the caller has no possibility to define the type parameters listed after:
template<typename T,
typename..., // firewall, absorbs all supplied arguments
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
}
Optionally, to prevent calling f
accidentally with too many template arguments, one can add a static_assert:
template<typename T,
typename... F,
typename aux = some_ugly_and_large_or_deep_template_struct_1<T>::type>
aux f(const aux&, const aux&)
{
static_assert(sizeof...(F)==0, "Too many template arguments");
// body, that uses perhaps more times my
// "aux" type
}
Usually, I can live with letting the user define types like aux
, being for example the return type where this can save you a cast.
Or you can replace the static_assert
with an enable_if
:
template<typename T,
typename... F, typename = typename std::enable_if<sizeof...(F)==0>::type,
typename aux = some_ugly_and_large_or_deep_template_struct<T>::type,>
aux f(const aux&, const aux&)
{
// body, that uses perhaps more times my
// "aux" type
}
这篇关于模板函数中的模板别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!