* #pragma for defined class
@ 2006-09-06 18:28 Wesley Smith
2006-09-06 19:01 ` John Love-Jensen
0 siblings, 1 reply; 2+ messages in thread
From: Wesley Smith @ 2006-09-06 18:28 UTC (permalink / raw)
To: gcc-help
Hi,
I'm having some issues with a setup with alot of include files in a
C++ project. In trying to debug it, I want to try and print out
whether or not classes have been defined in header files yet.
Here's the idea:
class Point
{
};
#ifndef Point
#error Point
#endif
clearly this doesn't work, becaus ePoint is a class and not a #define.
Is there a command that can pick up on whether a class has been
defined?
thanks,
wes
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: #pragma for defined class
2006-09-06 18:28 #pragma for defined class Wesley Smith
@ 2006-09-06 19:01 ` John Love-Jensen
0 siblings, 0 replies; 2+ messages in thread
From: John Love-Jensen @ 2006-09-06 19:01 UTC (permalink / raw)
To: Wesley Smith, MSX to GCC
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2006-09-06 19:01 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-09-06 18:28 #pragma for defined class Wesley Smith
2006-09-06 19:01 ` John Love-Jensen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).