Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to instruct a C++ compiler to skip rest of current file?

Once in a while some functionality has to be conditionally compiled. For example, there's class Logger that is only used when WITH_LOGGING macro is #defined:

// Logger.cpp
#ifdef WITH_LOGGING
#include <Logger.h>
// several hundred lines
// of class Logger
// implementation
// follows
#endif

which is not very convenient - unless the reader scrolls through the file he can't be sure that the matching #endif is position at the end of file and so the whole file contents is excluded with the #ifdef. I'd prefre to have something like this:

// Logger.cpp
#ifndef WITH_LOGGING
#GetOutOfThisFile
#endif
#include <Logger.h>
// several hundred lines
// of class Logger
// implementation
// follows

so that it's clear that once the WITH_LOGGING is not #defined the compiler just skips the rest of the file.

Is something like that possible in C++?

like image 336
sharptooth Avatar asked Sep 01 '25 17:09

sharptooth


1 Answers

An easy way to clarify this would be to put the implementation in another file which is included:

file Logger.cpp:

#ifdef WITH_LOGGING
 #include <Logger.h>
 #include "logger.impl"
#endif

file logger.impl:

// several hundred lines
// of class Logger
// implementation
// follows
like image 82
wallyk Avatar answered Sep 04 '25 05:09

wallyk