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). | 
| 
 | Optimizes memory allocation for  | 
| 
 | Defined if C++11  | 
| 
 | Defines compile-time constants in a way that is compatible with older C++ versions, so ensures compatibility with pre-C++11 compilers. | 
| 
 | Defines the version of the Boost.Filesystem library being used, to ensure compatibility between different versions of that library. | 
| 
 | Defined if C++11 lambda expressions are not available, so allows for providing alternative function objects for compatibility. | 
| 
 | Disables deprecated features in Boost.Filesystem. | 
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 | 
|---|---|
| 
 | Asserts an expression, throwing an error in debug builds. | 
| 
 | Performs a compile-time assertion. | 
| 
 | Performs a compile-time assertion, with an added message. | 
| 
 | Like  | 
| 
 | Enables a custom assertion handler. | 
| 
 | Enables the debugging of handlers when using Boost.Asio. | 
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 | 
|---|---|
| 
 | 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. | 
| 
 | Define when the system is byte-order little-endian. | 
| 
 | Define when the system is byte-order big-endian. | 
Examples
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
}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 | 
|---|---|
| 
 | Disables multi-threading support in Boost. | 
| 
 | Indicates if Boost is built with threading support. | 
| 
 | 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. | 
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;
    }
}