我正在使用 STL C++0x 容器调试一些 C++ 解析器(工具链是 GCC 4.7.0)。
而且由于 STL 重新绑定(bind)代码很难理解,我需要以某种方式打印 typedef 的完整实例化链,如 std::vector<T>::reference
.当然,它只解析为 T&
, 但在它通过 __gnu_cxx
中的至少 7 个不同模板之前和其他内部结构。
因此,我期待在模板错误的情况下打印出类似的内容,但对于编译器实例化的每个类。 可能吗? GCC 插件,也许...
UPD:好吧,我已经手动实例化了所有必需的模板。似乎没有什么好的方法可以自动执行此操作,除了将一些调试 printf 语句插入 GCC 代码本身。
最佳答案
由于您使用的是 GCC 4.7,我假设您使用的系统可以针对您的代码运行 clang。 Clang 的错误消息,特别是模板的错误消息非常好。
template class Example { Example(const T& t) : t_(t) {} T& t_; }; int a; Example e(a);
输出:
t.cpp:8:14: error: calling a private constructor of class 'Example' Example e(a); ^ t.cpp:3:5: note: implicitly declared private here Example(const T& t) : t_(t) {} ^ t.cpp:3:27: error: binding of reference to type 'int' to a value of type 'const int' drops qualifiers Example(const T& t) : t_(t) {} ^ ~ t.cpp:8:14: note: in instantiation of member function 'Example::Example' requested here Example e(a); ^ 2 errors generated.
关于c++ - 打印实例链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10396753/