Boost logo

Boost :

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


AMDG

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
#ifndef TEST_ODR_HPP_INCLUDED
#define TEST_ODR_HPP_INCLUDED

#include <iostream>
#include <cstdlib>

#ifndef SOME_MACRO
#define SOME_MACRO true
#endif

template<bool>
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";
                std::abort();
            }
        }
    };
    test_odr_impl_t test_odr_impl;
}

#endif

// 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 acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk