__PRETTY_FUNCTION__ in Visual C++
Asked Answered
N

2

20

While playing around with Visual C++ from Visual Studio 2017 (15.5.6 to be exact) I noticed that __PRETTY_FUNCTION__ from GCC seems to work! - to some degree at least…

It did show up along suggestions while typing: enter image description here

Also it had its value shown in tooltip as expected: enter image description here

And yet compiling the code using __PRETTY_FUNCTION__ results in an error:

error C2065: '__PRETTY_FUNCTION__': undeclared identifier

So, is there any way to make it work in Visual C++? Some include perhaps? Or some special compilation settings? (I think I'm using default ones.) Why would it work in shown examples, but not in actual use?!

Note, that I'm not looking for Visual C++ alternatives for __PRETTY_FUNCTION__. I know them already. I'm just surprised and curious about the behavior here.

Nesmith answered 18/2, 2018 at 23:52 Comment(6)
Remember that Intellisense is a different compiler than what is used to compile your code.Tao
@Tao Is it? That I didn't knew... Kind of surprising actually! (To me at least...) Where do you know this from? And does it explain this effect or is just a possible reason?Nesmith
That's interesting. I could understand this happening with some third party IDE like Eclipse etc. But apparently Microsoft's own IDE knows something about __PRETTY_FUNCTION__, even though their compiler doesn't support it?Karyn
blogs.msdn.microsoft.com/vcblog/2009/05/27/…Tao
Yeah that's "pretty" funny to be fairSometimes
EDG's C++ Front End documentation mentions it on page 71 - "1.13 Predefined Macro" together with Micsosoft's own __FUNCSIG__.Draggletailed
D
19

So, is there any way to make it work in Visual C++? Some include perhaps? Or some special compilation settings? (I think I'm using default ones.) Why would it work in shown examples, but not in actual use?!

No.

The Visual Studio uses the Edison Design Group C++ Front End for the InteliSense, as explained in the Visual C++ Team Blog's Rebuilding Intellisense and here, and not the Microsoft C++ compiler. This means that some of the features available to the Intellisense are not available in compile time.

EDG's C++ Front End documentation mentions that its supports some of the GCC pre-defines like __PRETTY_FUNCTION__ on page 71 - "1.13 Predefined Macros" together with Microsoft's __FUNCSIG__.

You can even see the version of the EDG by typing __EDG_VERSION__ and hovering over it.

Draggletailed answered 21/2, 2018 at 10:47 Comment(3)
There is a way to make it sort-of work. Include the following: #define __PRETTY_FUNCTION__ __FUNCSIG__Annora
full macro check #ifdef _MSC_VER #define __PRETTY_FUNCTION__ __FUNCSIG__ #endifMulticolored
Pedantic but note that __PRETTY_FUNCTION__ is a reserved identifier, best practice not to #define itTruett
S
8

You can emulate it pretty well by putting this in some common header (for example, your precompiled header, if you're using that):

#if !defined(__PRETTY_FUNCTION__) && !defined(__GNUC__)
#define __PRETTY_FUNCTION__ __FUNCSIG__
#endif

It doesn't evaluate to exactly the same as PRETTY_FUNCTION, but it's a human-readable representation of the function name with argument types.

Segovia answered 3/5, 2021 at 18:44 Comment(4)
This does not work in gcc/clang. You will get the error: gcc.godbolt.org/z/774vnn5KrTaking
For people who just copy-and paste, I improved the solution to be GNU compatible out of the box.Segovia
It seems that __PRETTY_FUNCTION__ is not a regular macro, and its availability cannot be checked like that with #ifdef or similar.Sanguineous
@LauriNurmi It can not be a macro because the preprocessor does not understand what a function it. Both C and C++ specify __func__ as a magic identifier and so are all extensions/alternatives of it.Denbrook

© 2022 - 2024 — McMap. All rights reserved.