模板函数的小烦恼
Small annoyance with template function
好的,假设我有一个这样的函数
template <typename T>
void Func(std::vector<T> x, T alpha = 1)
{
// ... do stuff
}
,我想把它用在一个复杂的类型上,像这样
std::vector<std::complex<double>> x;
Func(x, 5.5);
然后编译器抱怨(VS2010) template parameter 'T' is ambiguous
,因为它是could be 'double' or 'std::complex<double>'
。明显的修复,像这样调用
Func(x, std::complex<double>(5.5));
但是,那,我不想要。为什么不能自动转换为复杂类型?
它不起作用的原因是因为第一个参数导致T
被推断为std::complex<double>
,而第二个参数导致它被推断为double
。它在演绎论证时根本不考虑转换。显然,这使得演绎有歧义。
您可以在identity
帮助器的帮助下强制第二个参数类型为不可演绎的:
template <typename T>
struct identity { typedef T type; };
template <typename T>
void Func(std::vector<T> x, typename identity<T>::type alpha = 1)
{
// ... do stuff
}
这个怎么样?
template<typename T>
struct identity {
typedef T type;
};
template <typename T>
void Func(std::vector<T> x, typename identity<T>::type alpha = 1)
{
// ... do stuff
}
第二个实参将不参与模板形参的推导,将使用vector模板实参
如果需要一个默认实参的类型与包含的类型不同,只需使用两种不同的类型:
template <class T, class U>
void Func(vector<T> x, U y);
如果你不喜欢标识帮助器,只是希望它是可转换的,试试这个:
template <typename T, typename U>
void Func(const std::vector<T>& x, U alphaU = 1)
{
const T alpha(alphaU); // Convert to T
// ... do stuff
}
更新:
回复你的评论:我在http://liveworkspace.org上尝试了几个编译器,这个代码对我来说很好:
#include <vector>
#include <complex>
template <typename T, typename U>
void Func(const std::vector<T>& x, U alphaU)
{
const T alpha(alphaU); // Convert to T
(void) x;
(void) alpha;
}
template <typename T>
void Func(const std::vector<T>& x)
{
Func( x, 1 );
}
int main()
{
std::vector<std::complex<double>> v(10);
Func( v, 5.5 );
Func( v );
}
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 可以X X(T ..)导致函数声明,并具有烦恼的解析
- 强制将最令人烦恼的解析的歧义消除为函数声明
- 模板函数的小烦恼