COIN-OR::LEMON - Graph Library

Opened 7 years ago

Last modified 16 months ago

#373 new enhancement

Compile time assertion

Reported by: kpeter Owned by: alpar
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 7 years ago by alpar

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 7 years ago by kpeter

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

comment:3 Changed 5 years ago by alpar

  • Milestone changed from LEMON 1.3 release to LEMON 1.4 release

comment:4 Changed 4 years ago by ggabor

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 16 months ago by alpar

  • Milestone changed from LEMON 1.4 release to LEMON 1.5 release
Note: See TracTickets for help on using tickets.