0

I'm receiving a 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 with >10GB memory usage from g++ 4.2.1.

I'm trying to create a helper function that subtracts a scalar from each element of a vector. Thinking generically at first, I planned to have the specific vector storage type a template parameter. That being said, I'm realizing I don't know how to make the definition work for both ublas and std vectors at this point (without specialization). But I'm still wondering what is happening / the reason for my compile troubles?

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

Is there a bug in boost or in my code?

// 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;
}
NoahR
  • 1,417
  • 3
  • 18
  • 33
  • 2
    It seems to me that your `operator - ()` is obviously setting up an infinite recursion by invoking itself when doing `vec - scalar_v`. Or am I missing something? – Andy Prowl Feb 11 '13 at 22:05
  • oh gosh, yes. because when I call ublas::operator-(vec,scalar_v) I do not have the error. – NoahR Feb 11 '13 at 22:08
  • I don't think this question leaves any value for anyone else then, what do I do? delete? – NoahR Feb 11 '13 at 22:10
  • I expanded the comment into an answer, if you want you can accept it. Otherwise, feel free to delete the question. – Andy Prowl Feb 11 '13 at 22:12

1 Answers1

1

Your operator - () function is setting up an infinite template instantiation recursion by invoking itself when doing vec - scalar_v. Although the name of the parameter type is scalar_t, this is just a name and it can match any type, including a scalar_vector<scalar_t>.

Thus, the line xbnew = operator - (x, bnew) will cause the instantiation of:

operator - <DVEC, double>()

The last line in that operator template will in turn cause the instantiation of:

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

Then, the last line in the same operator template will cause the instantiation of:

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

And so on until the compiler reaches the maximum instantiation recursion depth.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • Thanks for the explanation Andy. That makes sense. Sorry I didn't see it for myself. I intend to fix with more explicit scope resolution in the function body. – NoahR Feb 11 '13 at 22:13
  • Is there a way to circumvent this ? I mean could one get [this](http://coliru.stacked-crooked.com/a/de977996dc0f2d6c) to compile w/o increasing the instantiation depth? – Lorah Attkins Mar 26 '16 at 22:19