detecting ODR violations

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

Don't be mad. How about using precompiled headers? -Sid Sacek -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Steven Watanabe Sent: Saturday, July 12, 2008 11:57 PM To: boost@lists.boost.org Subject: [boost] detecting ODR violations 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 _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

On Sun, Jul 13, 2008 at 5:56 AM, Steven Watanabe <watanabesj@gmail.com> wrote:
<snip>
Comments? Any better ideas?
I think that the next version of the gcc linker (known as GOLD) is meant to (optionally?) catch ODR violations. It should already be working, and this functionality already available. I do not know if there any tar ball available yet or if you have to fetch it from gcc svn. HTH, -- gpd
participants (3)
-
Giovanni Piero Deretta
-
Sid Sacek
-
Steven Watanabe