如果定义一个简单的类型traits来检测正确的返回类型,如下所示
realType
template <typename T>
struct realType
{ using type = T; };
template <typename T>
struct realType<std::complex<T>>
{ using type = T; };
这个
bar()
函数可以写成
template <typename T>
T bar (T const & x)
{ return foo<T, typename realType<T>::type>(x); }
#include <complex>
template <typename T>
struct realType
{ using type = T; };
template <typename T>
struct realType<std::complex<T>>
{ using type = T; };
template <typename numeric_t, typename real_t>
numeric_t foo(numeric_t x)
{
real_t y = abs(x);
return x/y;
}
template <typename T>
T bar (T const & x)
{ return foo<T, typename realType<T>::type>(x); }
int main ()
{
using type1 = decltype(bar<float>(1.0f));
using type2 = decltype(bar<std::complex<double>>(1.0));
static_assert(std::is_same<type1, float>{}, "!");
static_assert(std::is_same<type2, std::complex<double>>{}, "!");
}
离题建议。
foo()
函数有两种模板类型,但只有第一种(
numeric_t
)可以从输入参数推断(
x
). 因此,你必须明确这两个方面。
如果反转模板类型的顺序
template <typename real_t, typename numeric_t>
numeric_t foo(numeric_t x)
{
real_t y = abs(x);
return x/y;
}
你可以打电话
foo()
只解释第一个,让编译器推导第二个;所以
条形图()
template <typename T>
T bar (T const & x)
{ return foo<typename realType<T>::type>(x); }
--编辑--
根据aschepler的建议(谢谢!)你可以推导出
real_t
输入
foo()
从…起
数字\u t
; 因此,无需切换这两种模板类型,您就可以编写
foo()
如下所示
template <typename numeric_t,
typename real_t = typename realType<numeric_t>::type>
numeric_t foo(numeric_t x)
{
real_t y = abs(x);
return x/y;
}
和
条形图()
简单地成为
template <typename T>
T bar (T const & x)
{ return foo(x); }