Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to allow higher template recursion limit in MSVC?

I’m writing template metaprogramming code in C++ to generate a look-up table for embedded programming purposes (lack of FPU). I’ve been using MSVC for prototyping.

When trying to generate the final 128×128, 2 bytes per cell LUT, I’m getting error C1202: recursive type or function dependency context too complex.

But for 100×100, it gets generated in seconds and all is well. I haven’t been able to find how to increase the template recursion limit in MSVC on the web.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

According to this question from 2014 asking the same thing, Visual C++ – Set the depth of template instantiation, there was none, but I hope things have changed since and someone here may know about it.

Here is the code for context:

const size_t tableSize = 128;
const float unit = 80.;
using Cell = std::pair<uint8_t, uint8_t>;

constexpr auto f = [](uint8_t x, uint8_t y) -> Cell {
    float _x = x / unit;
    float _y = y / unit;

    float norm2 = std::sqrt(_x * _x + _y * _y);
    float norm2Min1 = norm2 < 1 ? 1 : norm2;
    return {(uint8_t)(x / norm2Min1), (uint8_t)(y / norm2Min1) };
};

template<uint8_t rowNo, uint8_t columnNo> struct Row {
    Cell cell = f(columnNo, rowNo);
    Row<rowNo, columnNo + 1> rest;
};
template<int rowNo> struct Row<rowNo, tableSize-1> {
    Cell cell = f(tableSize-1, rowNo);
};

template<int rowNo> struct Table {
    Row<rowNo, 0> row;
    Table<rowNo + 1> rest;
};
template<> struct Table<tableSize-1> {
    Row<tableSize-1, 0> row;
};

struct LUT {
    Table<0> table;

    Cell precomputedF(uint8_t x, uint8_t y) {
        return ((Cell*)(&table))[y * tableSize + x];
    }
};

int main()
{
    LUT lut;
    int size = sizeof(LUT);

    std::cout << size << "\n\n";

    for (int x = 0; x < tableSize; x++) {
        for (int y = 0; y < tableSize; y++) {
            auto result = lut.precomputedF(x, y);
            std::cout << "(" << (int)result.first << "," << (int)result.second << ")" << " ";
        }
        std::cout << "\n";
    }
}```

>Solution :

Don’t use templates. Use constexpr

using Table = std::array<std::array<Cell, tableSize>, tableSize>;
constexpr  Table make_LUT() {
    Table table;
    for(size_t x=0; x<tableSize; x++) {
        for(size_t y=0; y<tableSize; y++)
            table[y][x] = f(x,y);
    }
    return table;
};
constexpr Table LUT = make_LUT();
constexpr Cell precomputedF(uint8_t x, uint8_t y) {
    return LUT[y][x];
}

http://coliru.stacked-crooked.com/a/0d406313f9bec451

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading