How to comment lines automatically in release mode? - c++

I need to have some lines of code "active" in debug mode only, and ignored in release mode.
Is there a way to do something like this:
#include <iostream>
using namespace std;
#ifdef _TEST_
#define _cerr cerr
#else
#define _cerr // cerr
#endif
int main() {
_cerr << "TEST message" << endl;
}
So that when _TEST_ is not defined, some lines are commented, or removed from the code. I know that comments are processed before the rest, so this code is wrong. But how can I get the behaviour I need without using #ifdefs explicitely?

You can use a macro for this:
#ifdef _TEST_
#define DEBUG_ONLY(x) x;
#else
#define DEBUG_ONLY(x)
#endif
int main() {
DEBUG_ONLY(cerr << "TEST message" << endl)
}

If what you are after is debug logging which is removed in release builds, you can do something like:
#ifdef _TEST_
#define LOG(x) (cerr << x << endl)
#else
#define LOG(x)
#endif
...
int main() {
LOG("TEST message");
}

Use this:
#ifdef _TEST_
#define DEBUG_TEST(x) x
#else
#define DEBUG_TEST(x)
#endif
int main() {
DEBUG_TEST(_cerr << "TEST message" << endl);
}

ifdefs are the way to go. How else would you know if the compiler is in release vs debug mode? When else besides during the preprocessing stage would that be communicated? At what other stage could you decide to remove/add code (besides during template generation). Hey maybe we can use template generation... but you still have to key off the ifdef somehow to control your template.
Maybe there's a really slick way to do this that I'm not thinking of, but everyone knows/uses ifdefs for this purpose. If you throw them a curveball its only going to drastically increase the human cost of maintaining your code.
Stick with ifdefs.

This here basically does what you are asking for:
#ifdef _TEST_
#define _cerr cerr
#else
#define _cerr if (1) {} else cerr
#endif
But don't be surprised if you for example get compiler warnings about ambiguous else if you write something like this:
if (something)
_cerr << "Why?" << std::endl;
You should always be aware of the fact that this _cerr is in fact a non-trivial macro.

Defining _cerr to nothing will fail the compilation. You could instead define a macro that you exclude while in release mode.
For example:
#ifdef _TEST_
#define LOG_ERROR(log) cerr << log << endl;
#else
#define LOG_ERROR(log)
#endif
Then in your code:
int main() {
LOG_ERROR("TEST message");
}

int main() {
#ifdef _TEST_
_cerr << "TEST message" << endl;
#endif
}

No. Absolutely, no.
Try a variation on this:
#ifdef _TEST_
ostream& _cerr = cerr;
#else
ostringstream _cerr;
#endif
(Basically you would want a stream which just discards its input.)

A nicer solution for the "no logging in release" is the following class:
class NullStream {
template<typename T> NullStream& operator<< const(T&) { }
};
Use:
#ifdef DEBUG
#define CERR std::cerr
#else
#define CERR NullStream()
#endif

Create your own NULL stream.
#include <iostream>
class NullStream {};
template<typename T>
NullStream& operator <<(NullStream& n,T const& data) {return n;}
NullStream& operator <<(NullStream& n,std::ostream& (*)(std::ostream&)) {return n;}
#ifdef _TEST_
#define myerr std::cerr
#else
NullStream myerrstream;
#define myerr myerrstream
#endif
int main()
{
myerr << "Hi" << std::endl;;
myerr << std::endl;;
}

Related

how to avoid using ifdef to make code more testable

I am creating a log file for logging information. It has a switch to turn it on or off.
#define LOG_FILE
#ifdef LOG_FILE
#define LOG(MESSAGE1, MESSAGE2) log(MESSAGE1, MESSAGE2);
#else
#define LOG(MESSAGE1, MESSAGE2)
#endif
Now I can control logging using LOG_FILE switch. I was wondering there are any other way achieve similar feature which is more testable?
You can use templates like this:
#include <string>
#include <iostream>
#define USE_LOGGING true
template<bool>
inline void log(const std::string& message, const std::string& verbosity) {}
template<>
inline void log<true>(const std::string& message, const std::string& verbosity) {
std::cout << verbosity << ": " << message << "\n";
}
constexpr auto logMessage = log<USE_LOGGING>;
int main(int argc, char** argv) {
logMessage("Test", "Warning");
}
By this you will have both versions available, using logMessage gives you the global settings, alternatively you can use log directly which is a local definition of how to use logging.

Nifty counter in precompiled header does not work

To fix a problem caused by the static (de-)initialization order fiasco I tried to use the Nifty Counter idiom aka Schwartz Counter. I noticed, however, that it does not work if the header file containing the static initializer is included inside Visual Studio's precompiled header. In such a case I see in the debugger, that the constructor of the static initializer is called after the constructor of the other static object.
If I declare an additional static initializer right after #include "stdafx.h" or edit the precompiled header to not contain the declaration it works as expected.
Any ideas what might cause this?
EDIT
I was finally able to reproduce this problem in a new dummy project:
Foo.cpp
#include "stdafx.h"
#include "Foo.h"
#include <cassert>
#include <iostream>
#ifdef NIFTY
static int SchwartzCounter; // zero initialized at load time
static typename std::aligned_storage<sizeof(Foo), alignof (Foo)>::type foo_buf;
Foo& foo = reinterpret_cast<Foo&>(foo_buf);
#else
Foo foo;
#endif
Foo::Foo()
{
std::cout << __func__ << std::endl;
}
Foo::~Foo()
{
std::cout << __func__ << std::endl;
}
void Foo::doSomething()
{
std::cout << __func__ << std::endl;
assert(x == 42);
}
#ifdef NIFTY
FooInitializer::FooInitializer()
{
std::cout << __func__ << std::endl;
if (SchwartzCounter++ == 0)
{
new (&foo) Foo();
}
}
FooInitializer::~FooInitializer()
{
std::cout << __func__ << std::endl;
if (--SchwartzCounter == 0)
{
(&foo)->~Foo();
}
}
#endif
Foo.h
#pragma once
class Foo
{
public:
Foo();
~Foo();
void doSomething();
private:
int x = 42;
};
#ifdef NIFTY
extern Foo& foo;
static struct FooInitializer {
FooInitializer();
~FooInitializer();
} fooInitializer;
#else
extern Foo foo;
#endif
Bar.cpp
#include "stdafx.h"
#include "Foo.h"
#include "Bar.h"
#include <cassert>
#include <iostream>
#ifdef NIFTY
static int SchwartzCounter; // zero initialized at load time
static typename std::aligned_storage<sizeof(Bar), alignof (Bar)>::type bar_buf;
Bar& bar = reinterpret_cast<Bar&>(bar_buf);
#else
Bar bar;
#endif
Bar::Bar()
{
std::cout << __func__ << std::endl;
foo.doSomething();
}
Bar::~Bar()
{
std::cout << __func__ << std::endl;
}
void Bar::doSomething()
{
std::cout << __func__ << std::endl;
assert(x == 42);
}
#ifdef NIFTY
BarInitializer::BarInitializer()
{
std::cout << __func__ << std::endl;
if (SchwartzCounter++ == 0)
{
new (&bar) Bar();
}
}
BarInitializer::~BarInitializer()
{
std::cout << __func__ << std::endl;
if (--SchwartzCounter == 0)
{
(&bar)->~Bar();
}
}
#endif
Bar.h
#pragma once
class Bar
{
public:
Bar();
~Bar();
void doSomething();
private:
int x = 42;
};
#ifdef NIFTY
extern Bar& bar;
static struct BarInitializer {
BarInitializer();
~BarInitializer();
} barInitializer;
#else
extern Bar bar;
#endif
stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// If the following define is commented out, the nifty counter idiom is not used
#define NIFTY
// If the following include is commented out while the nifty counter idiom is used, the initialization order is correct
#include "Foo.h"

Disable exception working for boost::smart_ptr but not for boost::property_tree

I'm using boost 1.57.
I need to disable exception support in my not widely-used proprietary compiler.
When I needed to use boost::smart_ptr, the following steps worked like a charm:
Disabled C++11 support using the following user.hpp file (compiling with -DBOOST_USER_CONFIG="<user.hpp>"):
#define BOOST_HAS_NRVO
#define BOOST_NO_COMPLETE_VALUE_INITIALIZATION
#define BOOST_NO_CXX11_AUTO_DECLARATIONS
#define BOOST_NO_CXX11_AUTO_MULTIDECLARATIONS
#define BOOST_NO_CXX11_CHAR16_T
#define BOOST_NO_CXX11_CHAR32_T
#define BOOST_NO_CXX11_CONSTEXPR
#define BOOST_NO_CXX11_DECLTYPE
#define BOOST_NO_CXX11_DECLTYPE_N3276
#define BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
#define BOOST_NO_CXX11_DELETED_FUNCTIONS
#define BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
#define BOOST_NO_CXX11_FINAL
#define BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS
#define BOOST_NO_CXX11_LAMBDAS
#define BOOST_NO_CXX11_LOCAL_CLASS_TEMPLATE_PARAMETERS
#define BOOST_NO_CXX11_NOEXCEPT
#define BOOST_NO_CXX11_NULLPTR
#define BOOST_NO_CXX11_RANGE_BASED_FOR
#define BOOST_NO_CXX11_RAW_LITERALS
#define BOOST_NO_CXX11_REF_QUALIFIERS
#define BOOST_NO_CXX11_RVALUE_REFERENCES
#define BOOST_NO_CXX11_SCOPED_ENUMS
#define BOOST_NO_CXX11_STATIC_ASSERT
#define BOOST_NO_CXX11_TEMPLATE_ALIASES
#define BOOST_NO_CXX11_UNICODE_LITERALS
#define BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
#define BOOST_NO_CXX11_USER_DEFINED_LITERALS
#define BOOST_NO_CXX11_VARIADIC_MACROS
#define BOOST_NO_CXX11_VARIADIC_TEMPLATES
#define BOOST_NO_SFINAE_EXPR
#define BOOST_NO_TWO_PHASE_NAME_LOOKUP
Informs boost libraries not to use exceptions: -DBOOST_NO_EXCEPTIONS -DBOOST_EXCEPTION_DISABLE
Resulting in the following compilation line: MyCOMPILER -DBOOST_USER_CONFIG="<user.hpp>" -DBOOST_NO_EXCEPTIONS -DBOOST_EXCEPTION_DISABLE -c Foo.cpp -o Foo.o
Then the following piece of code successfully compiled:
#include <iostream>
#include <string>
#include <boost/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared.hpp>
namespace boost {
void throw_exception(std::exception const& e) {
std::cerr << "Fake exception: " << e.what() << "\n";
std::exit(255);
}
}
class Foo {
private:
boost::shared_ptr<int> ptr;
public:
bool bar(const std::string file_name) {
ptr = boost::make_shared<int>();
return true;
}
};
But when I've tried to use the above method with the following source code, the compilation goes wrong:
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
namespace boost {
void throw_exception(std::exception const& e) {
std::cerr << "Fake exception: " << e.what() << "\n";
std::exit(255);
}
}
class Foo {
public:
bool bar(const std::string file_name) {
boost::property_tree::ptree prop_tree;
boost::property_tree::read_ini(file_name, prop_tree);
return !prop_tree.empty();
}
};
Resulting the following errors for instance:
"boost\boost/optional/optional.hpp", line 1047: Error: #312: no suitable user-defined conversion from "boost::bad_optional_access" to "const std::exception" exists
throw_exception(bad_optional_access());
^
"boost\boost/optional/optional.hpp", line 1055: Error: #312: no suitable user-defined conversion from "boost::bad_optional_access" to "const std::exception" exists
throw_exception(bad_optional_access());
^
"boost\boost/type_index/stl_type_index.hpp", line 138: Error: #312: no suitable user-defined conversion from "std::runtime_error" to "const std::exception" exists
boost::throw_exception(std::runtime_error("Type name demangling failed"));
^
"boost\boost/property_tree/ini_parser.hpp", line 168: Error: #540: support for exception handling is disabled; use --exceptions to enable
try {
^
Finally the question:
Why in the first case all works OK, but not in the second?
How can I get boost::property_tree to compile properly without exception support? Is this even possible?

boost::property_tree without exceptions

I need to parse some INI files. To do this, I'm trying to use boost::property_tree, but in my system exceptions are not allowed.
How can I disable the exception support when using boost::property_tree?
If there's no way to do this, any recommendations for another library are very appreciated.
After the answer from #sehe, I tried this piece of code, but without success:
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
namespace boost {
void throw_exception(std::exception const& e) {
std::cerr << "Fake exception: " << e.what() << "\n";
std::exit(255);
}
}
class Foo {
public:
bool bar(const std::string file_name) {
boost::property_tree::ptree prop_tree;
boost::property_tree::read_ini(file_name, prop_tree);
return !prop_tree.empty();
}
};
The compilation line code uses the following parameters:
-c -DBOOST_USER_CONFIG="<user.hpp>" -DBOOST_NO_EXCEPTIONS -DBOOST_EXCEPTION_DISABLE -I.\toolchain\boost -I.\include Foo.cpp
And finally, the user.hpp file:
#define BOOST_HAS_NRVO
#define BOOST_NO_COMPLETE_VALUE_INITIALIZATION
#define BOOST_NO_CXX11_AUTO_DECLARATIONS
#define BOOST_NO_CXX11_AUTO_MULTIDECLARATIONS
#define BOOST_NO_CXX11_CHAR16_T
#define BOOST_NO_CXX11_CHAR32_T
#define BOOST_NO_CXX11_CONSTEXPR
#define BOOST_NO_CXX11_DECLTYPE
#define BOOST_NO_CXX11_DECLTYPE_N3276
#define BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
#define BOOST_NO_CXX11_DELETED_FUNCTIONS
#define BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
#define BOOST_NO_CXX11_FINAL
#define BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS
#define BOOST_NO_CXX11_LAMBDAS
#define BOOST_NO_CXX11_LOCAL_CLASS_TEMPLATE_PARAMETERS
#define BOOST_NO_CXX11_NOEXCEPT
#define BOOST_NO_CXX11_NULLPTR
#define BOOST_NO_CXX11_RANGE_BASED_FOR
#define BOOST_NO_CXX11_RAW_LITERALS
#define BOOST_NO_CXX11_REF_QUALIFIERS
#define BOOST_NO_CXX11_RVALUE_REFERENCES
#define BOOST_NO_CXX11_SCOPED_ENUMS
#define BOOST_NO_CXX11_STATIC_ASSERT
#define BOOST_NO_CXX11_TEMPLATE_ALIASES
#define BOOST_NO_CXX11_UNICODE_LITERALS
#define BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX
#define BOOST_NO_CXX11_USER_DEFINED_LITERALS
#define BOOST_NO_CXX11_VARIADIC_MACROS
#define BOOST_NO_CXX11_VARIADIC_TEMPLATES
#define BOOST_NO_SFINAE_EXPR
#define BOOST_NO_TWO_PHASE_NAME_LOOKUP
Some errors returned by compiler:
"boost/optional/optional.hpp", line 1047: Error: #312: no suitable user-defined conversion from "boost::bad_optional_access" to "const std::exception" exists
throw_exception(bad_optional_access());
^
"boost/property_tree/string_path.hpp", line 221: Error: #312: no suitable user-defined conversion from "boost::property_tree::ptree_bad_path" to "const std::exception" exists
BOOST_PROPERTY_TREE_THROW(ptree_bad_path("Path syntax error", *this));
^
"boost/property_tree/detail/ptree_implementation.hpp", line 78: Error: #742: namespace "boost" has no actual member "iterator_core_access"
friend class boost::iterator_core_access;
^
PS: I'm not using a widely-available standard compiler, but it's successfully compiling the boost::smart_ptr, using the same process described above.
You can configure Boost Exception to not use exceptions:
http://www.boost.org/doc/libs/1_58_0/libs/exception/doc/configuration_macros.html
You need to define BOOST_NO_EXCEPTIONS and further implement your own C-style exception handling function, like e.g.
void throw_exception(std::exception const& e) {
std::cerr << "Fake exception: " << e.what() << "\n";
std::exit(255);
}
Live On Coliru
#define BOOST_NO_EXCEPTIONS
#include <iostream>
#include <boost/property_tree/ini_parser.hpp>
namespace boost {
void throw_exception(std::exception const& e) {
std::cerr << "Fake exception: " << e.what() << "\n";
std::exit(255);
}
}
using namespace boost::property_tree;
int main() {
ptree pt;
read_ini(std::cin, pt);
write_ini(std::cout, pt.get_child("section5"));
}
Compiled with
g++ -std=c++11 -O3 -Wall -pedantic main.cpp -fno-exceptions
This prints
./test <<< "[section1]"
Fake exception: No such node (section5)

Compiler acts as if a preprocessor directive isn't defined

I have a header file and its cpp file (Error.h, Error.cpp). The cpp file performs a check on a preprocessor directive but it always fails.
Error.h:
/*
Optional macros:
AE_EXIT_AT_ERROR
AE_CONSOLE_WRITE_AT_ERROR
*/
#pragma once
extern void aeError(const char *str, int code=1);
extern void aeAssert(bool b, const char *failStr = "assertion failed");
Error.cpp:
#include "Error.h"
#include <stdexcept>
#ifdef AE_CONSOLE_WRITE_AT_ERROR
#include <iostream>
#endif
void aeError(const char *str, int code)
{
#ifdef AE_CONSOLE_WRITE_AT_ERROR
std::cout << str << std::endl;
#endif
throw std::runtime_error(str);
#ifdef AE_EXIT_AT_ERROR
std::exit(code);
#endif
}
void aeAssert(bool b, const char *failStr)
{
if(!b)
aeError(failStr);
}
main.cpp:
//define both macros:
#define AE_CONSOLE_WRITE_AT_ERROR
#define AE_EXIT_AT_ERROR
#include "Error.h"
//rest of code
//...
both std::cout << str << std::endl; and std::exit(code); don't get compiled (I checked it "manually", although they are also marked gray by the IDE, which is VS2010).
What might be the cause of this?
main.cpp and Error.cpp are different translation units. You define the macro only for main.cpp, not for Error.cpp.
You should either put your #define directives in a header file included by both .cpp files, or define these macros in project settings/makefile.