0

I have a Windows Phone 8 Solution with a Windows Runtime Component project. In the WinRT code I want to use __FUNCTION__ for logging from a C++ class. However, __FUNCTION__ is not defined whereas __LINE__ is.

Intellisense only suggests __FUNCTIONW__ to use. But when I jump to the definition of __FUNCTIONW__ in crtdefs.h I can see that __FUNCTION__ is not defined there either:

definition of __FUNCTIONW__ in crtdefs.h

I have read Why would __FUNCTION__ be undefined? but that did not help me (or I did not understand the problem described there correctly)

How could __FUNCTION__ not be defined? I thought it is build in to the compiler...


Update:

OK, I learned that __FUNCTION__ is actually never colored. Yet I get an error when I type:

TCHAR* f = _T(__FUNCTION__);

It says:

Error: Identifier "L__FUNCTION__" is undefined

Maybe something is wrong with my UNICODE setup? Is there a special header that I need to include?

Community
  • 1
  • 1
codingFriend1
  • 6,487
  • 6
  • 45
  • 67
  • Have you tried telling the compiler to use C++11? Or have you checked the MSDN site? – Thomas Matthews Jun 04 '14 at 15:58
  • `__FUNCTION__` is not a macro - so it's not defined in any header. `__FUNCTIONW__` is just a wide-string wrapper around `__FUNCTION__`. Oddly enough, in VS2013, intellisense doesn't recognize `__FUNCTION__` but it does recongnize `__LINE__`. At least with a standard Win32 project. – 001 Jun 04 '14 at 16:08
  • Windows Runtime uses C++/CX, it is not plain old C++, http://msdn.microsoft.com/en-us/library/hh699871.aspx – Matt Jun 04 '14 at 16:08

1 Answers1

4

It works fine when I try it in a C++ Phone 8 app. Do note that you might be confuzzled by __FUNCTION__ not appearing in the IntelliSense auto-complete list. You can only see __FUNCTIONW__. Which is a flaw, the macro is predefined in the compiler but that was overlooked for the IS parser. Since the definition of the macro doesn't appear anywhere in the included headers, the parser won't ever see a definition for it (like it does for __FUNCTIONW__) and you'll see it missing from the list.

I can't confirm if this is a known bug, the search function at connect.microsoft.com isn't good enough to filter the 6600 hits that match "function". You can file the bug if you wish.


Update after edit: you are using the ancient TCHAR macros in your code. There's a trick to get the correct macro but I'm not going to document it. This all stopped being relevant well over 10 years ago when everybody stopped creating programs that could still run on one of the legacy Windows 9x versions. Certainly entirely pointless on a phone.

You in fact want to use __FUNCTIONW__ (without the _T), it produces the Unicode string for the function name. It is a const wchar_t*.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks! You are right. The missing coloring confused me. Knowing that, I guess my problem is actually somewhere else. When I do `TCHAR* f = _T(__FUNCTION__);` I get an error `Identifier L__FUNCTION__ is undefined` (I'll update the question to reflect that) – codingFriend1 Jun 05 '14 at 08:38
  • Yes, I am indeed porting some very old code to Windows Phone :-) I will rewrite it then. Thank you very much! – codingFriend1 Jun 05 '14 at 09:09