简体   繁体   English

如何在编译时调试模板参数?

[英]How to debug template arguments at compile-time?

I have a piece of code that pretty much reduces down to: 我有一段代码,几乎减少到:

template<class T> struct MyStruct;  // No definition by default
template<class T> struct MyStruct<T *> { ... };  // Specialization for pointers

Now somewhere in my code, I'm getting an instantiation of MyStruct<T> that happens to be undefined (no C++0x/011, no Boost... nothing fancy, just plain C++03): 现在在我的代码中的某个地方 ,我得到了一个MyStruct<T>的实例化,它恰好是未定义的(没有C ++ 0x / 011,没有Boost ......没什么特别的,只是普通的C ++ 03):

error C2027: use of undefined type 'MyStruct<T>'

The trouble is, I have no idea where this is being caused , because the code that's doing the instantiation is itself a template, and called from numerous places, with different arguments. 麻烦的是, 我不知道在哪里 ,这是所引起 ,因为这是做实例代码本身就是一个模板,从许多地方调用,使用不同的参数。

Is there a way to somehow figure out what T is at compile-time, so I can understand the error messages better? 有没有办法以某种方式弄清楚T在编译时是什么 ,所以我能更好地理解错误信息?

(Sorry, I forgot to mention: Visual Studio 2008.) (对不起,我忘了提及:Visual Studio 2008.)

I believe you're using MSVC++, if so, then see the output window, it might have more info printed, especially the line number along with the filename. 我相信你正在使用MSVC ++,如果是这样,然后看到输出窗口,它可能会打印更多信息,特别是行号和文件名。 Once you know the file and line number, you can start from there. 一旦知道文件和行号,就可以从那里开始。

Output window usually prints everything, like how and with what template argument(s), a template is instantiated. 输出窗口通常打印所有内容,例如如何以及使用什么模板参数,模板实例化。 Everything step by step. 一切都在循序渐进。 Those messages are very useful when debugging. 调试时这些消息非常有用。

As you found yourself, enabling /WL prints more detail messages in the output window. 正如您自己发现的那样,启用/ WL会在输出窗口中打印更多详细信息。

I know you said no C++11, but you may want to consider, since C++03 code is backwards compatible in all C++11 compliant compilers, to use the static_assert feature of C++11 to debug your code ... if you must do the final compile with a C++03 compiler, then you can always create a #define and use the #ifdef and #endif pre-processor macros to make sure that the static_assert feature does not cause problems in earlier compilers that do not support C++11 features. 我知道你说没有C ++ 11,但你可能要考虑,因为C ++ 03代码在所有符合C ++ 11的编译器中向后兼容,要使用C ++ 11的static_assert特性来调试你的代码。 ..如果必须使用C ++ 03编译器进行最终编译,那么您始终可以创建#define并使用#ifdef#endif预处理器宏来确保static_assert功能不会导致早期出现问题不支持C ++ 11功能的编译器。

See the MSDN docs here for more info. 有关详细信息,请参阅此处MSDN文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM