在MS Visual Studio 2010监视窗口中评估模板函数

Evaluate Template Function in MS Visual Studio 2010 Watch Window

本文关键字:评估 函数 窗口 监视 MS Visual Studio 2010      更新时间:2023-10-16

问题:有什么方法可以从MS Visual Studio Watch 2010窗口评估模板函数吗?

详细信息:我知道如果我有以下功能:

void Test (CString& sSample)
{
   OutputDebugString (_T("The value of Sample is ") + sSample);
}

我可以在监视窗口中使用,并在调试时在输出窗口中打印输出。但是,我看到当使用下面这样的模板函数时,这是不起作用的:

template<class T>
void Test (T& t)
{
   OutputDebugString (_T("The value when T is string is: ") + t);
}

我能以某种方式实现它吗?我知道autoexp.bat对变量很有帮助。但对它在函数中的行为一无所知。有什么帮助吗?

调试模板函数没有问题。这里的问题是OutputDebugString,它的参数是字符串,所以"Text1"+"Text2"不起作用,"Text1"+123不起作用。它恰好与CString一起工作,否则它就不是真正的模板材料。

这将适用于VS 2013(在C++=>语言选项中,启用RTTI),它可能不适用于VS 2010:

template<class T> void Test2(T& t)
{
    OutputDebugStringA( "typeid(t).name=" );
    OutputDebugStringA( typeid(t).name() );
    OutputDebugStringA( "n" );
}
int i = 0;
CString str = "str";
Test(i);
Test(str);

无论如何,只要试着让模板函数更简单,这样它就可以处理整数、浮点等。或者让它区分数字和字符串。