Friday, 15 April 2011

c++ - How to understand and fix recursive template instantiation error using boost ublas? -



c++ - How to understand and fix recursive template instantiation error using boost ublas? -

i'm receiving compiler error

/developer/boost/boost/numeric/ublas/expression_types.hpp:184:16: fatal error: recursive template instantiation exceeded maximum depth of 512

from clang++ or indefinite compiling >10gb memory usage g++ 4.2.1.

i'm trying create helper function subtracts scalar each element of vector. thinking generically @ first, planned have specific vector storage type template parameter. beingness said, i'm realizing don't know how create definition work both ublas , std vectors @ point (without specialization). i'm still wondering happening / reason compile troubles?

the problem readily demonstrated below code when complied boost library, e.g. g++ -i/path/boost bug_demo.cpp

is there bug in boost or in code?

// demo code not compiler, instead error recursive template instantiation //--------------------------------------------------------- #include <boost/numeric/ublas/vector.hpp> namespace ublas = boost::numeric::ublas; // boost ublas implementation 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); homecoming vec - scalar_v; } // non-template version works fine. /* dvec operator-( const dvec & vec, double scalar ) { ublas::scalar_vector<double> scalar_v(vec.size(), scalar); homecoming vec - scalar_v; } */ dvec vectorfunc( const dvec& x ) { double bnew=0.0; dvec x_bnew; x_bnew = operator-(x,bnew); homecoming x_bnew; } int main() { dvec inputvector(2); inputvector[0] = 1.0; inputvector[1] = 2.0; dvec output = vectorfunc( inputvector ); homecoming 0; }

your operator - () function setting infinite template instantiation recursion invoking when doing vec - scalar_v. although name of parameter type scalar_t, name , can match type, including scalar_vector<scalar_t>.

thus, line xbnew = operator - (x, bnew) cause instantiation of:

operator - <dvec, double>()

the lastly line in operator template in turn cause instantiation of:

operator - <dvec, scalar_vector<double>>()

then, lastly line in same operator template cause instantiation of:

operator - <dvec, scalar_vector<scalar_vector<double>>>()

and on until compiler reaches maximum instantiation recursion depth.

c++ boost ublas

No comments:

Post a Comment