c++ - C1202 : recursive type or function dependency context too complex

标签 c++ algorithm compiler-errors c++17

我尝试编写一个函数,该函数会回调大小为 n 的每个排列:

template<typename F>
void permutationsCallback(int n, F f) {
    if (n == 1) {
        f(std::vector<int>{0});
    }
    else {
        permutationsCallback(n - 1, [n, &f](std::vector<int> p) {
            p.emplace_back(n - 1);
            f(p);
            while (--p.back() >= 0) {
                for (int k = 0; k < n - 1; k++) {
                    p[k] += p[k] == p.back();
                }
                f(p);
            }
        });
    }
}

(例如,我希望 permutationsCallback(3, f) 调用 f({0,1,2}, f({0,2, 1}), f({1,2,0}), f({1,0,2}), f({ 2,0,1}), f({2,1,0}).)

但是在使用 MCSV 编译时出现错误

fatal error C1202: recursive type or function dependency context too complex

我该如何解决这个问题?

最佳答案

模板内部存在无限递归。 即使您知道 else block 不会在运行时被调用,但在编译时,编译器需要无休止地知道哪些代码会进入 else block 。

例如,您可以考虑将 n 改为模板参数,并实现 0 情况,就像 MSVC 在其页面上针对此错误消息所解释的那样:

https://learn.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/fatal-error-c1202?view=msvc-160

引用上面的链接:

// C1202b.cpp
// compile with: /c
template<int n>
class Factorial : public Factorial<n-1> {
public:
   operator int () {
      return Factorial <n-1>::operator int () * n;
   }
};

template <>
class Factorial<0> {
public:
   operator int () {
      return 1;
   }
};

Factorial<7> facSeven;

关于c++ - C1202 : recursive type or function dependency context too complex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67038822/

相关文章:

c++如何访问父类中的函数?

c++ - 从 SIFT 的 OpenCV 匹配中获取关键点的坐标

c++ - 连接文件而不复制其内容

algorithm - 对复发和大 O 感到困惑

algorithm - O(n) 和 O(log(n)) 之间的区别 - 哪个更好,O(log(n)) 究竟是什么?

matlab - 在Matlab中使用mex编译C++时,如何摆脱stdafx.h错误?

javascript - 为什么编译器找不到 'axios'

c++ - 使用删除时随机崩溃

java - 处理具有大量对象的 Union-Find 算法

Angular 2 : Supplied parameters do not match any signature of call target