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.
-
- Notes
-
Most of the macros do not take any parameters. Those that do have
(..)in the tables below.The code in this topic was written and tested using Microsoft Visual Studio (Visual C++ 2022, Console App project) with Boost version 1.88.0.
Library Inclusion and Linking
| Macro | Description | Defined In | Include Required? |
|---|---|---|---|
|
Disables automatic linking for all Boost libraries. |
|
Usually No (via other Boost headers) |
|
Enables diagnostic output for automatic linking. |
|
Usually No |
|
Forces all Boost libraries to be linked dynamically. |
|
Usually No |
Configuration and Compiler Detection
| Macro | Description | Defined In | Include Required? |
|---|---|---|---|
|
Defines the Boost version as an integer (for example, 107800 for Boost 1.78.0). |
|
Yes |
|
Identifies the platform (for example, Windows, Linux). |
|
Yes |
|
Identifies the compiler in use (for example, gcc, msvc). |
|
Yes |
|
Identifies the standard library (for example, libstdc++, msvc). |
|
Yes |
|
Optimizes memory allocation for |
|
Yes (if used directly) |
|
Defined if C++11 smart pointers are not available. |
|
Yes |
|
Defines compile-time constants compatible with older C++ versions. |
|
Yes |
|
Defines the version of Boost.Filesystem being used. |
|
Usually No (via |
|
Defined if C++11 lambdas are not available. |
|
Yes |
|
Disables deprecated Boost.Filesystem features. |
|
Usually No |
Examples
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;
}
Using BOOST_PLATFORM, BOOST_COMPILER, and BOOST_STDLIB for diagnostics by identifing the system’s platform, compiler, and standard library.
#include <iostream>
#include <boost/config.hpp>
int main() {
std::cout << "Platform: " << BOOST_PLATFORM << std::endl;
std::cout << "Compiler: " << BOOST_COMPILER << std::endl;
std::cout << "Standard Library: " << BOOST_STDLIB << std::endl;
return 0;
}
Using BOOST_VERSION to ensure version compatibility when using Boost.
#include <iostream>
#include <boost/version.hpp>
int main() {
std::cout << "Boost version: "
<< (BOOST_VERSION / 100000) << "." // Major version
<< (BOOST_VERSION / 100 % 1000) << "." // Minor version
<< (BOOST_VERSION % 100) << std::endl; // Patch version
return 0;
}
Using BOOST_SP_USE_QUICK_ALLOCATOR to optimize shared_ptr.
#define BOOST_SP_USE_QUICK_ALLOCATOR // Define before including Boost.SmartPtr
#include <boost/smart_ptr.hpp>
#include <iostream>
int main() {
boost::shared_ptr<int> ptr1(new int(42));
boost::shared_ptr<int> ptr2 = ptr1;
std::cout << "Shared pointer value: " << *ptr1 << std::endl;
return 0;
}
Debugging and Assertions
| Macro | Description | Defined In | Include Required? |
|---|---|---|---|
|
Asserts an expression in debug builds. |
|
Yes |
|
Performs a compile-time assertion. |
|
Yes |
|
Compile-time assertion with a message. |
|
Yes |
|
Like |
|
Yes |
|
Enables a custom assertion handler. |
|
Yes |
|
Enables handler debugging in Boost.Asio. |
|
Usually No (via |
Examples
Using BOOST_ASSERT, BOOST_STATIC_ASSERT, and BOOST_VERIFY for debugging.
#include <boost/assert.hpp>
#include <boost/static_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
}
Using BOOST_ASIO_ENABLE_HANDLER_TRACKING to help debug asynchronous operations by tracking handlers.
#define BOOST_ASIO_ENABLE_HANDLER_TRACKING // Enable debugging before including Boost.Asio
#include <boost/asio.hpp>
#include <iostream>
int main() {
boost::asio::io_context io;
std::cout << "Boost.Asio handler tracking enabled!\n";
return 0;
}
Preprocessor Utilities
| Macro | Description | Defined In | Include Required? |
|---|---|---|---|
|
Repeats a macro expansion. A modern equivalent is |
|
Yes |
|
Preprocessor |
|
Yes |
|
Expands into comma-separated parameters. A modern equivalent is |
|
Yes |
Example
Using BOOST_PP_REPEAT to generate repeated code.
#include <boost/preprocessor/repetition/repeat.hpp>
#include <iostream>
#define PRINT_N(z, n, text) std::cout << text << " " << n << "\n";
int main() {
BOOST_PP_REPEAT(5, PRINT_N, "Iteration");
}
The output from the above example should be:
Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Cross-Platform Compatibility
| Macro | Description | Defined In | Include Required? |
|---|---|---|---|
|
Defined when compiling on Windows. |
|
Yes (include |
|
Defined when compiling on Linux. |
|
Yes |
|
Defined when compiling on a Unix-like system. |
|
Yes |
|
Defined when system is little-endian. |
|
Yes |
|
Defined when system is big-endian. |
|
Yes |
Examples
Using BOOST_WINDOWS, BOOST_LINUX, and BOOST_UNIX for platform-specific code.
#include <iostream>
#include <boost/config.hpp>
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
}
Using Using BOOST_LITTLE_ENDIAN and BOOST_BIG_ENDIAN for proper handling of byte order in serialization.
#include <iostream>
#include <boost/config.hpp>
int main() {
#if defined(BOOST_LITTLE_ENDIAN)
std::cout << "System is little-endian.\n";
#elif defined(BOOST_BIG_ENDIAN)
std::cout << "System is big-endian.\n";
#else
std::cout << "Unknown endianness.\n";
#endif
return 0;
}
Threading and Synchronization
| Macro | Description | Defined In | Include Required? |
|---|---|---|---|
|
Disables Boost threading support. |
|
Yes |
|
Indicates Boost threading support is enabled. |
|
Yes |
|
Forces Boost.Thread to use static library. |
|
Usually No (via |
|
Forces Boost.Thread to use shared library. |
|
Usually No |
|
Enables future support in Boost.Thread. |
|
Usually No |
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 | Defined In | Include Required? |
|---|---|---|---|
|
Portable exception throwing macro. |
|
Yes |
|
Disables exception handling. |
|
Yes |
Examples
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";
}
}
Using BOOST_NO_EXCEPTIONS to ensure compatibility with exception-free environments.
#include <iostream>
#include <boost/config.hpp>
void safe_function() {
#ifndef BOOST_NO_EXCEPTIONS
throw std::runtime_error("Something went wrong!");
#else
std::cerr << "Exceptions are disabled. Handling error manually.\n";
#endif
}
int main() {
try {
safe_function();
} catch (const std::exception& e) {
std::cerr << "Caught exception: " << e.what() << std::endl;
}
}