如何在MSVC中允许更高的模板递归限制?

问题描述 投票:0回答:1

我正在用 C++ 编写模板元编程代码,以生成用于嵌入式编程目的的查找表(缺少 FPU)。我一直在使用 MSVC 进行原型设计。

当尝试生成最终的 128x128、每个单元 2 字节 LUT 时,我收到错误 C1202:递归类型或函数依赖上下文太复杂。

但是对于 100x100,它会在几秒钟内生成,一切都很好。我在网上没找到如何增加 MSVC 中的模板递归限制。

根据2014年提出同样问题的问题,Visual C++ - 设置模板实例化的深度,没有,但我希望事情已经改变,这里有人可能知道它。

这是上下文代码:

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";
    }
}```
c++ templates visual-c++ template-meta-programming lookup-tables
1个回答
0
投票

不要使用模板。使用

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

© www.soinside.com 2019 - 2024. All rights reserved.