-1

Where in GCC is the behavior of C++’s __PRETTY_FUNCTION__ defined? I want to replicate its functionality in Visual Studio.

It’s my understanding that I could replace __PRETTY_FUNCTION__ with either __FUNCTION__ or __FUNCSIG__,¹ but that, then, I’d lose the ability to compile the code on macOS.

How do include __PRETTY_FUNCTION__’s functionality in code that I want to be able to compile in, say, both Visual Studio and Xcode.


  1. Apologies if the answer to my question is in the text I’m linking to or in the resources it mentions.
Lucas
  • 456
  • 2
  • 11
  • 15
  • So you want to define `__PRETTY_FUNCTION__` so your code compiles on MSVC, rather than defining `__FUNCTION__` so it compiles on MacOS? – Tas May 23 '19 at 21:48
  • Instead of saying "make it like `__PRETTY_FUNCTION__`" which may change with g++ version and platform, why don't you give examples of what you are getting and what you want instead? BTW `__func__` is the standard-compliant name for `__FUNCTION__`, it should be supported on Mac. – Ben Voigt May 23 '19 at 21:51
  • *"Where in GCC is the behavior of C++’s `__PRETTY_FUNCTION__` defined?"* - what do you want to know - where it is in the source code, where GCC docs discuss it...? The question doesn't seem at all relevant to your seeming functional requirements, which are easily satisfied with the `#if` / `#else` per-OS selection everyone's suggesting. – Tony Delroy May 23 '19 at 21:56
  • @BenVoigt I‘m trying to run [this code](https://web.archive.org/web/20180926143331id_/bioinfo.umassmed.edu/bootstrappers/guides/main/cxx_numeric_limits.html#numeric-limits) while asking the question without so much specificity that the answer couldn’t be used outside of that code’s context. Neither using `__func__` in Visual Studio nor using `__FUNCTION__` in Xcode replicated `__PRETTY_FUNCTION__`’s behavior for me. – Lucas May 25 '19 at 15:47
  • @Lucas: Of course it does not. That code requires your compiler to generate a function name that is character-for-character identical to the compiler you designed it with, while the Standard specifically calls out the name produced by `__func__` (and by RTTI) as implementation-defined. Your code may even break when using a different gcc version. – Ben Voigt May 25 '19 at 15:51
  • You are likely, however, to be happier starting with `typeid(T).name()` than with `__PRETTY_FUNCTION__`. – Ben Voigt May 25 '19 at 15:54

1 Answers1

6

Define your own macro that refers to __FUNCTION__ or __PRETTY_FUNCTION__ based on your compiler:

#ifdef _MSC_VER
#define MY_FUNCTION_MACRO __FUNCTION__ // or __FUNCSIG__
#else
#define MY_FUNCTION_MACRO __PRETTY_FUNCTION__
#endif

And then use MY_FUNCTION_MACRO throughout your cross-patform code.

Note that MY_FUNCTION_MACRO will still produce different strings from between VC++ and GCC/Clang. This is unavoidable and gets you as close as you can get.

For example:

int main()
{
   std::cout << MY_FUNCTION_MACRO << std::endl;
}

will produce "main" when using __FUNCTION__ or "int __cdecl main(void)" when using __FUNCSIG__ in VC++, whereas __PRETTY_FUNCTION__ produces "int main()" in GCC/Clang.

cscrimge
  • 376
  • 2
  • 9
  • I think this is close to correct. I’m wondering why (even after using your `MY_FUNCTION_MACRO` solution and changing the `uint`s to `unsigned int`s) the output of each line in [`main()` in this code](https://web.archive.org/web/20180926143331id_/bioinfo.umassmed.edu/bootstrappers/guides/main/cxx_numeric_limits.html#numeric-limits), that uses the `typeStr` template, still outputs `indeterminate` when run using Visual Studio. – Lucas May 25 '19 at 15:40
  • @Lucas that code you linked is trying to parse the result string of `__PRETTY_FUNCTION__`. `__FUNCSIG__` will get you close in Visual Studio, but doesn't produce exactly the same output. – cscrimge Jun 05 '19 at 00:35