mardi 8 mai 2018

Ambiguous recursive template function

In C++11, I need to call a function recursively from 0,...,n (where n is a compile time constant). This is the structure of the problem, which appears to be fatally flawed:

template<size_t i>
struct Int {
};

template<size_t d, typename C, typename X>
constexpr X eval(const C &c, const X &x, const Int<d> &, const Int<n> &) {
    return 1;
}

template<size_t d, typename C, typename X, size_t i>
constexpr X eval(const C &c, const X &x, const Int<d> &, const Int<i> &) {
    return x * eval(c, x, Int<d>(), Int<i + 1>());
}

The initial call to the function looks like this:

eval(c, x, Int<d>(), Int<0>());

The error message is of the form

error: call of overloaded 'eval(const Eigen::Matrix<double, 1, 7>&, const double&, {anonymous}::Int<1ul>, {anonymous}::Int<5ul>)' is ambiguous

My understanding was that in the last line, x * eval(c, x, Int<d>(), Int<i + 1>());, when i+1 = n, then the first function which returns 1 would be chosen, but the compiler says that the call is ambiguous. Could somebody explain why? and how to fix this?

Aucun commentaire:

Enregistrer un commentaire