COIN-OR::LEMON - Graph Library

Opened 14 years ago

Last modified 8 years ago

#373 new enhancement

Compile time assertion

Reported by: Peter Kovacs Owned by: Alpar Juttner
Priority: major Milestone: LEMON 1.5 release
Component: core Version: hg main
Keywords: Cc:
Revision id:

Description

Compile time assertion would be useful in some cases (instead of LEMON_ASSERT).

A possible solution is the following:

template <bool> struct STATIC_ASSERT_FAILURE;
template <> struct STATIC_ASSERT_FAILURE<true> {};
template<int x> struct static_assert_test{};

#define LEMON_COMPILE_ASSERT(x) \
   typedef static_assert_test<\
       sizeof(STATIC_ASSERT_FAILURE< (bool)( x ) >)>\
           _static_assert_typedef_

It could be used like that:

LEMON_COMPILE_ASSERT(numeric_limits<T>::is_integer);

Change History (5)

comment:1 Changed 14 years ago by Alpar Juttner

It would indeed be useful in some cases. How meaningful/understandable is the error message emitted by the compiler? For example how will it comment the line

LEMON_COMPILE_ASSERT(numeric_limits<double>::is_integer);

?

comment:2 Changed 14 years ago by Peter Kovacs

boost/static_assert.h in the Boost library implements a solution. Maybe we should check it.

comment:3 Changed 12 years ago by Alpar Juttner

Milestone: LEMON 1.3 releaseLEMON 1.4 release

comment:4 Changed 11 years ago by Gábor Gévay

C++11 has a keyword for this: http://en.cppreference.com/w/cpp/language/static_assert

The latest versions of GCC, Clang, ICC, MSVC all support it. But maybe an ifdef could hide it, if the compiler doesn't support it.

comment:5 Changed 8 years ago by Alpar Juttner

Milestone: LEMON 1.4 releaseLEMON 1.5 release
Note: See TracTickets for help on using tickets.