我收到编译错误
/Developer/boost/boost/numeric/ublas/expression_types.hpp:184:16: fatal error: recursive template instantiation exceeded maximum depth of 512
来自clang ++的或使用g ++ 4.2.1中的> 10GB内存使用情况进行无限期编译。
我正在尝试创建一个辅助函数,它从向量的每个元素中减去一个标量。首先想到的是,我计划让特定的矢量存储类型成为模板参数。话虽这么说,我意识到我不知道如何在这一点上使定义适用于ublas
和std
向量(没有专门化)。但我仍然想知道发生了什么/我编译问题的原因?
当遵循升级库时,使用以下代码可以很容易地证明该问题,例如:
g++ -I/path/boost bug_demo.cpp
boost或我的代码中是否有错误?
// demo code does not compiler, instead error for recursive template instantiation
//---------------------------------------------------------
#include <boost/numeric/ublas/vector.hpp>
namespace ublas = boost::numeric::ublas; // boost ublas will be the implementation for basic vector container
typedef ublas::vector<double> DVEC; // double vector shorthand
// ********* problem function ***********
template <typename vec_t, typename scalar_t>
vec_t operator-( const vec_t & vec, scalar_t scalar )
{
ublas::scalar_vector<scalar_t> scalar_v(vec.size(), scalar);
return vec - scalar_v;
}
// this non-template version works fine.
/* DVEC operator-( const DVEC & vec, double scalar )
{
ublas::scalar_vector<double> scalar_v(vec.size(), scalar);
return vec - scalar_v;
}
*/
DVEC vectorFunc( const DVEC& x )
{
double bnew=0.0;
DVEC x_bnew;
x_bnew = operator-(x,bnew);
return x_bnew;
}
int main()
{
DVEC inputVector(2);
inputVector[0] = 1.0; inputVector[1] = 2.0;
DVEC output = vectorFunc( inputVector );
return 0;
}
答案 0 :(得分:1)
您的operator - ()
函数在执行vec - scalar_v
时通过调用自身来设置无限模板实例化递归。虽然参数类型的名称为scalar_t
,但这只是一个名称,它可以匹配任何类型,包括scalar_vector<scalar_t>
。
因此,行xbnew = operator - (x, bnew)
将导致实例化:
operator - <DVEC, double>()
该运算符模板中的最后一行将导致实例化:
operator - <DVEC, scalar_vector<double>>()
然后,同一个运算符模板中的最后一行将导致实例化:
operator - <DVEC, scalar_vector<scalar_vector<double>>>()
依此类推,直到编译器达到最大实例化递归深度。