Boost Macros
Boost provides many useful macros that help with configuration, debugging, portability, and convenience when working with the libraries.
There are thousands of defined macros (refer to the Boost Macro Reference). However, here are some of the most commonly used, and some sample code showing how to work them into your code.
Library Inclusion and Linking
Macro | Description |
---|---|
|
Disables automatic linking for all Boost libraries. |
|
Enables diagnostic output for automatic linking. |
|
Used to specify which libraries should be included when using B2. |
|
Forces all Boost libraries to be linked dynamically. |
Configuration and Compiler Detection
Macro | Description |
---|---|
|
Defines the Boost version as an integer (for example, 107800 for Boost 1.78.0). |
|
Identifies the platform (for example, Windows, Linux). |
|
Identifies the compiler in use (for example, gcc, msvc). |
|
Identifies the standard library (for example, libstdc++, msvc). |
|
Disables exception handling in Boost. |
|
Disables multi-threading support in Boost. |
|
Indicates if Boost is built with threading support. |
Example
The BOOST_STDLIB
macro is part of Boost.Config and identifies the standard library implementation being used (for example, GNU libstdc++, Microsoft STL, or libc++). This is useful when writing code that needs to adapt to different standard library behaviors. BOOST_STDLIB
expands to a string representing the detected standard library.
#include <boost/config.hpp>
#include <iostream>
int main() {
std::cout << "Detected Standard Library: " << BOOST_STDLIB << std::endl;
#ifdef BOOST_LIBSTD_GNU
std::cout << "Using GNU libstdc++" << std::endl;
#elif defined(BOOST_LIBSTD_MSVC)
std::cout << "Using Microsoft STL (MSVC Standard Library)" << std::endl;
#elif defined(BOOST_LIBSTD_LIBCXX)
std::cout << "Using libc++ (Clang Standard Library)" << std::endl;
#else
std::cout << "Using an unknown or unsupported standard library" << std::endl;
#endif
return 0;
}
Debugging and Assertions
Macro | Description |
---|---|
|
Asserts an expression, throwing an error in debug builds. |
|
Performs a compile-time assertion. |
|
Like |
|
Enables a custom assertion handler. |
Example
Using BOOST_ASSERT
, BOOST_STATIC_ASSERT
, and BOOST_VERIFY
for debugging.
#include <boost/assert.hpp>
#include <iostream>
void process(int value) {
BOOST_ASSERT(value > 0 && "Value must be positive"); // Runtime assertion
}
int main() {
BOOST_STATIC_ASSERT(sizeof(int) == 4); // Compile-time assertion
int x = 10;
BOOST_VERIFY(x != 0); // Always checks in release mode
process(-1); // Will trigger an assertion failure
}
Preprocessor Utilities
Macro | Description |
---|---|
|
Repeats a macro expansion a specified number of times. |
|
Implements an if-like construct at preprocessing time. |
|
Expands into a comma-separated list of parameters. |
Cross-Platform Compatibility
Macro | Description |
---|---|
|
Defined when compiling on Windows. |
|
Defined when compiling on Linux. |
|
Defined when compiling on a Unix-like system. |
|
Disables deprecated features in Boost.Filesystem. |
Example
Using BOOST_WINDOWS
, BOOST_LINUX
, and BOOST_UNIX
for platform-specific code.
#include <iostream>
int main() {
#ifdef BOOST_WINDOWS
std::cout << "Running on Windows\n";
#elif defined(BOOST_LINUX)
std::cout << "Running on Linux\n";
#elif defined(BOOST_UNIX)
std::cout << "Running on a Unix-like system\n";
#else
std::cout << "Unknown platform\n";
#endif
}
Threading and Synchronization
Macro | Description |
---|---|
|
Forces Boost.Thread to be used as a static library. |
|
Forces Boost.Thread to be used as a shared library. |
|
Enables std::future-like functionality in Boost.Thread. |
Example
Using BOOST_HAS_THREADS
to ensure threading support is enabled.
#include <boost/thread.hpp>
#include <iostream>
void threadFunc() {
std::cout << "Hello from thread!\n";
}
int main() {
#ifdef BOOST_HAS_THREADS
boost::thread myThread(threadFunc);
myThread.join();
#else
std::cout << "Threads are not supported.\n";
#endif
}
Exception Handling
Macro | Description |
---|---|
|
A portable way to throw exceptions that integrates with |
|
Disables exception handling. |
Example
Using BOOST_THROW_EXCEPTION
to throw exceptions in a portable way.
#include <boost/exception/exception.hpp>
#include <boost/exception/all.hpp>
#include <iostream>
struct MyException : virtual boost::exception, virtual std::exception {
const char* what() const noexcept override {
return "A Boost exception occurred!";
}
};
void riskyOperation() {
BOOST_THROW_EXCEPTION(MyException());
}
int main() {
try {
riskyOperation();
} catch (const MyException& e) {
std::cout << "Caught exception: " << e.what() << "\n";
}
}