简体   繁体   English

如何在不使用对象的情况下连接字符串,整数和浮点数?

[英]How to concatenate strings, integers and floating point numbers without using objects?

I'd like to add information in a crash dump file, in case my application crashes. 我想在崩溃转储文件中添加信息,以防我的应用程序崩溃。

Therefore I've created a __try - __except clause: 因此,我创建了一个__try __except子句:

__try
{
  Do_Something();
}
__except (ShowCrashdumpInformation(_T(__FUNCTION__));

Instead of just __FUNCTION__ , I'd like to add more information, but how can I do that? 我不仅要添加__FUNCTION__ ,还要添加更多信息,但是我该怎么做呢?

The simpliest way is to use a CString , but this is blocked because of compiler error C2712 (Cannot use __try in functions that require object unwinding). 最简单的方法是使用CString ,但这由于编译器错误C2712而被阻止(无法在需要对象展开的函数中使用__try)。

So, I'd like to use LPCTSTR strings (which are widely used in my application). 因此,我想使用LPCTSTR字符串(在我的应用程序中广泛使用)。

As a result it should look like ( CString alternative): 结果,它看起来应该像( CString替代):

CString temp; temp.Format(_T("Do_Something, int=[%d], float=[%f], string=[%s]), iParam, fParam, strParam);

Do anybody have an idea? 有人有主意吗?
Thanks 谢谢

You could use preprocessor macros to "stringify" the standard __LINE__ macro, and rely on the compiler adjacent string-literal concatenation. 您可以使用预处理器宏来“字符串化”标准__LINE__宏,并依赖于编译器相邻的字符串-文字级联。

Perhaps something like this: 也许是这样的:

#define STRx(x) #x
#define STR(x) STRx(x)

#define FILE_FUNCTION_LINE (__FILE__ ":" __FUNCTION__ ":" STR(__LINE__))

...

ShowCrashdumpInformation(_T(FILE_FUNCTION_LINE))

As long as you have literal values, you could use the STR macro to "stringify" them and then use adjacent string concatenation. 只要您具有文字值,就可以使用STR宏对它们进行“字符串化”,然后使用相邻的字符串连接。

It's not possible using variables though, only using literal values. 但是,仅使用文字值是不可能使用变量的。

By far the easiest solution is to simply sidestep the problem. 到目前为止,最简单的解决方案是简单地回避问题。 Just forward the exact arguments, not converted, to a (template) function which does the actual writing to file. 只需将未转换的确切参数转发给(模板)函数即可,该函数实际写入文件。 Since the __catch is not in the template function itself, but one level up the stack, you're safe. 因为__catch不在模板函数本身中,而是在堆栈的上一层,所以很安全。

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

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