Boost logo

Boost :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-07-12 23:56:49


I've been trying to work out a way to cause
an explicit error if configuration macros are defined
differently in different translation units. I really wish
I could cause a link error, but the best I've been able
to come up with so far is a runtime error

// test_odr.hpp

#include <iostream>
#include <cstdlib>

#ifndef SOME_MACRO
#define SOME_MACRO true

struct test_odr {
    static bool value;

template<bool b>
bool test_odr<b>::value = SOME_MACRO;

namespace {
    struct test_odr_impl_t {
        test_odr_impl_t() {
            if(test_odr<true>::value != SOME_MACRO) {
                std::cerr << "Error: SOME_MACRO defined differently in
two translation units.\n";
    test_odr_impl_t test_odr_impl;


// test_odr1.cpp
#include "test_odr.hpp"
int main() {}

// test_odr2.cpp
#define SOME_MACRO false
#include "test_odr.hpp"

Comments? Any better ideas?

In Christ,
Steven Watanabe

Boost list run by bdawes at, gregod at, cpdaniel at, john at