C++ Debug macro failing compile in release mode - c++

I'm using visual studio (2019) and I have this macro:
#if _DEBUG
#define EngineLog(vars, ...) Log(vars, ##__VA_ARGS__)
#endif
My log function this wraps around is:
template<typename... args>
static void Log(args && ... inputs)
{
std::osyncstream bout(std::cout);
bout << ENGINE_TAG;
([&]() {
bout << inputs;
} (), ...);
bout << "\n";
}
Obviously this works fine when I'm in debug mode, but when I use release mode I get:
Error C3861 'EngineLog': identifier not found
I'm unsure why the compiler takes issue here, I was told it would remove the macro from the code when not defined but it seems not to be doing that.

if _DEBUG is not defined, then EngineLog is an unresolved name. You need to add a definition for the other case.
#if _DEBUG
#define EngineLog(vars, ...) Log(vars, ##__VA_ARGS__)
#else
#define EngineLog(vars, ...) (0 && Log(vars, ##__VA_ARGS__))
#endif
The syntax used here prevents any potential warnings about the variadic arguments being unused while at the same time preventing both the function from being called and its parameters being evaluated.

Looks like you need a #else preprocessor case to define out the macro call with an empty expansion:
#if _DEBUG
#define EngineLog(vars, ...) Log(vars, ##__VA_ARGS__)
#else
#define EngineLog(vars, ...)
#endif

Related

How to define the constant _DEBUG in c++

When I compile and execute thes code, I get...
_DEBUG IS NOT defined
Why isn't the constant being shown as defined?
using namespace std;
int main() {
const bool _DEBUG = true;
#if defined _DEBUG
std::cout << "_DEBUG IS defined\n";
#else
std::cout << "_DEBUG IS NOT defined\n";
#endif // _DEBUG
}
#define _DEBUG
or
#define _DEBUG 1
The second method can be checked with #ifdef _DEBUG or #if _DEBUG. Usually _DEBUG is defined in compiler IDE profile.
#if defined TOKEN only checks if TOKEN is defined as a preprocessor macro, i.e. with #define TOKEN .... Here you have defined it as a (constant) variable, which is not the same thing.
const bool _DEBUG = true; defines a constant which is known to the compiler and not to the preprocessor.
The following check is executed by the preprocessor before the compiler kicks in, therefore it never sees _DEBUG constant.
#if defined _DEBUG
std::cout << "_DEBUG IS defined\n";
#else
std::cout << "_DEBUG IS NOT defined\n";
#endif // _DEBUG
To get rid of the issue, you should #define _DEBUG so that the preprocessor knows about the token.

#define real function name to __noop in Visual C++ 2013

I can do this in Visual C++ 2008 with Release (NDEBUG) setting:
debug.h
#ifdef _DEBUG
void debug_printf(const char* format, ...);
#else
#define debug_printf(format, v) __noop
#endif
debug.cpp
#include "stdafx.h" //#include "debug.h" is inside it
void debug_printf(const char* format, ...) {
//so much work here
}
but not anymore in Visual C++ 2013, I will get compile error in debug.cpp file. It seems I have to change the defining strategy in debug.h. But I wonder is there compiler setting to reenable again the old way?
Use a macro in the first case too, and let it call the actual function (which is named something different from the macro).
And in the second case, just have an empty macro body.
Use variadic macros.
Something like
#ifdef _DEBUG
# define debug_printf(fmt, ...) real_debug_printf(fmt, __VA_ARGS__)
#else
# define debug_printf(fmt, ...)
#endif
When _DEBUG is not defined, then the macro debug_printf is replaced by nothing (or rather, an empty line).

#error inside of #define - Possible in C++ (generate error WHEN calling macro MyMacro IF some constant is not defined)?

I want to define the macro, that based on some condition (existence of #define INITED, not the parameter of the macro) will return value, or generate compiler's error, like:
#error Not initialized!
I've tried (for myIdea.h):
#ifdef INITED
#define MyMacro(x) x->method(); //something with x
#else
#define MyMacro(x) #error Not initalized!
#endif
But that code generates error (not the one I wanted to) expected macro format parameter.
Note, that I don't want that code (working, but doing bit different thing):
#ifdef INITED
#define MyMacro(x) x->method(); //something with x
#else
#error Not initalized!
#endif
The code above will geneate error just when INITED won't be defined. I want to generate error only when I call to the MyMacro() AND INITED has not been yet defined.
I'm not the slave to the first code, but I want the result to work exactly the way I've described above (generate error WHEN calling macro MyMacro IF constant inited is not defined).
This is not possible. The preprocessor is just a very simple thing, it does not parse nested macros like that. The second pound (#) would not be understood as a nested macro by the preprocessor. The argument is pretty much handled as raw string.
You could however look into static assert with C++11 and on instead of your #error directive. You would be writing then something like this:
#ifdef INITED
#define MyMacro(x) x->method(); //something with x
#else
#define MyMacro(x) static_assert(false, "Not initalized!");
#endif

How to write the below piece of code using macro function?

How should I write the below piece of code using macro function?
#ifdef LOG_ENABLED
m_logger->Log(szType,szMessage);
#endif
I have done something like mentioned below and it resulted in error:-
#define _LOG_MSG_CND_BEGIN_ #ifdef LOG_ENABLED
#define _LOG_MSG_CND_END_ #endif
#define WriteLogMessage(szType,szMessage) \
{\
_LOG_MSG_CND_BEGIN_\
m_logger->Log(szType,szMessage);\
_LOG_MSG_CND_END_\
}
Please let me know how to write macro function for the above three piece of code without any errors.
I have used inline function for the same piece of code but while debugging i saw that the inline function was not getting treated as inline function and so i want to use macro function in this case.
Inline function which i used was as mentioned below:-
inline void WriteLogMessage(LOG_LEVEL szType, LPCTSTR szMessage){
#ifdef LOG_ENABLED
m_logger->Log(szType,szMessage);
#endif
}
The standard C preprocessor is a bit of an idiot and doesn't understand much at all. You want something like this:
#if defined LOG_ENABLED
#define WriteLogMessage(szType,szMessage) m_logger->Log(szType,szMessage)
#else
#define WriteLogMessage(szType,szMessage)
#endif
Preprocessor directives need to be the first thing in a line:
#define _LOG_MSG_CND_BEGIN_
#ifdef LOG_ENABLED
#define _LOG_MSG_CND_END_
#endif
#define WriteLogMessage(szType,szMessage) \
{\
_LOG_MSG_CND_BEGIN_\
m_logger->Log(szType,szMessage);\
_LOG_MSG_CND_END_\
}
EDIT: If you want macros to be expanded to other macros, that's not possible.

How can I use the TRACE macro in non-MFC projects?

I want to use the TRACE() macro to get output in the debug window in Visual Studio 2005 in a non-MFC C++ project, but which additional header or library is needed?
Is there a way of putting messages in the debug output window and how can I do that?
Build your own.
trace.cpp:
#ifdef _DEBUG
bool _trace(TCHAR *format, ...)
{
TCHAR buffer[1000];
va_list argptr;
va_start(argptr, format);
wvsprintf(buffer, format, argptr);
va_end(argptr);
OutputDebugString(buffer);
return true;
}
#endif
trace.h:
#include <windows.h>
#ifdef _DEBUG
bool _trace(TCHAR *format, ...);
#define TRACE _trace
#else
#define TRACE false && _trace
#endif
then just #include "trace.h" and you're all set.
Disclaimer: I just copy/pasted this code from a personal project and took out some project specific stuff, but there's no reason it shouldn't work. ;-)
If you use ATL you can try ATLTRACE.
TRACE is defined in afx.h as (at least in vs 2008):
// extern ATL::CTrace TRACE;
#define TRACE ATLTRACE
And ATLTRACE can be found in atltrace.h
You can try the DebugOutputString function. TRACE is only enabled in debug builds.
Thanks to these answers I have fixed my bug :-)
Here I share my TRACE macro in C++ based on ideas from Ferruccio and enthusiasticgeek.
#ifdef ENABLE_TRACE
# ifdef _MSC_VER
# include <windows.h>
# include <sstream>
# define TRACE(x) \
do { std::stringstream s; s << (x); \
OutputDebugString(s.str().c_str()); \
} while(0)
# else
# include <iostream>
# define TRACE(x) std::clog << (x)
# endif // or std::cerr << (x) << std::flush
#else
# define TRACE(x)
#endif
example:
#define ENABLE_TRACE //can depend on _DEBUG or NDEBUG macros
#include "my_above_trace_header.h"
int main (void)
{
int v1 = 123;
double v2 = 456.789;
TRACE ("main() v1="<< v1 <<" v2="<< v2 <<'\n');
}
Any improvements/suggestions/contributions are welcome ;-)
In my understanding wvsprintf has problem with formatting. Use _vsnprintf (or thcar version _vsntprintf ) instead
I've seen the following on the 'net:
#define TRACE printf
Developing this a little bit, came up with the following:
#ifdef _DEBUG
#define TRACE printf
#else
#define TRACE
#endif