C++ Compiler Errors: Nonsensical Errors from a Function Declaration

2024-12-12

A developer adding XAML support to a C++ application encountered a series of compiler errors simply by including the winrt/Windows.UI.Xaml.h header file. The errors stemmed from what appeared to be a normal function declaration: `template struct consume_Windows_UI_Xaml_IExceptionRoutedEventArgs { [[nodiscard]] auto ErrorMessage() const; };` The root cause was a pre-existing macro named ErrorMessage in the developer's project, conflicting with the function name. This macro created an ErrorMessageString object and returned a pointer to an error message string. The macro's lack of boundaries caused the compiler to misinterpret the function declaration as a macro invocation, resulting in errors like "not enough arguments." The solution involved disabling the macro using #pragma undef before including the header or removing the macro entirely and replacing it with an inline function.