Re: #pragma for defined class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Wes,

Try this (careful on the line wrap of the #define)...

------------------------
template <bool x> struct STATIC_ASSERTION_FAILURE;
template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
template <int x> struct static_assert_test { };
#define STATIC_ASSERT(x) \
typedef static_assert_test< sizeof(STATIC_ASSERTION_FAILURE< (bool)( x ) >)>
static_assert_typedef_ ## __LINE__

//class Foo; // Uncomment to see what happens.
//class Foo { }; // Uncomment to see what happens.
STATIC_ASSERT(sizeof(Foo) >= 0);
------------------------


Output if undeclared type Foo:
test.cpp:17: error: 'Foo' was not declared in this scope
test.cpp:17: error: template argument 1 is invalid

Output if forward declared (only) type Foo:
test.cpp:17: error: invalid application of 'sizeof' to incomplete type 'Foo'
test.cpp:17: error: invalid application of 'sizeof' to incomplete type
'STATIC_ASSERTION_FAILURE<false>'

Boost (www.boost.org) has the "boost/static_assert.hpp", which defines
BOOST_STATIC_ASSERT, and is more portable than the one I presented here.
Boost is free.  Boost is good.  Use Boost.

HTH,
--Eljay


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux