简体   繁体   中英

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
{
  Do_Something();
}
__except (ShowCrashdumpInformation(_T(__FUNCTION__));

Instead of just __FUNCTION__ , I'd like to add more information, but how can I do that?

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).

So, I'd like to use LPCTSTR strings (which are widely used in my application).

As a result it should look like ( CString alternative):

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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