[英]Using template typename in a nested function template
Alright, I'm struggling with templates. 好吧,我在模板上苦苦挣扎。 In this question I learned, that it is not possible to pass a type specifier to a function at all, so my next approach is passing the type inside
<>
. 在这个问题中,我了解到根本不可能将类型说明符传递给函数,因此我的下一个方法是在
<>
内部传递类型。
Imagine a function template foo<U>()
, which is member function of a template class A
. 想象一个函数模板
foo<U>()
,它是模板类A
成员函数。 So if I create an Object A<T> a
I can call a.foo<U>()
with any type. 因此,如果创建对象
A<T> a
可以调用任何类型的a.foo<U>()
。
How do I have to write an equivalent function template so I can pass a
and U
like wrappedFoo<U>(a)
? 我如何编写等效的函数模板,以便可以像
wrappedFoo<U>(a)
一样传递a
和U
?
IMPORTANT Needs to be C++98 compliant 重要事项需要符合C ++ 98
You might do the following: 您可以执行以下操作:
template <typename U, typename T>
XXXX /* See below */
WrappedFoo(/*const*/ A<T>& a)
{
return a.template foo<U>();
}
The hard part is the return type without decltype
of C++11. 困难的部分是没有C ++ 11的
decltype
的返回类型。
So if the return type really depends of parameters type, you can create a trait, something like: 因此,如果返回类型确实取决于参数类型,则可以创建一个特征,例如:
template <typename U, typename T>
struct Ret
{
typedef U type;
};
template <typename T> struct Ret<T, A<T> >
struct Ret
{
typedef bool type;
};
And then replace XXXX
by typename Ret<U, T>::type
然后用
typename Ret<U, T>::type
替换XXXX
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.