|
Boost : |
From: David Abrahams (abrahams_at_[hidden])
Date: 2000-07-23 10:07:55
This is mature and portable work which I hope we can capitalize on! I
remember when it was started originally over a year ago. Now that we're
getting seriously interested in template metaprogramming this work should
get used.
BTW, a build system for boost is currently in progress. I hope we'll be able
to support testing examples which are *supposed* to cause compilation
errors.
Finally, I want to make a last-ditch plea to consider other naming
conventions than "meta_" for this stuff. "Meta" is an extremely generic term
(no fair claiming that's appropriate in this case ;) ) which doesn't do the
facilities descriptive justice. Actually, if we put it all in a
sub-namespace of boost, I don't really care what we call it. Anyone is free
to write:
namespace ct = boost::metaprogramming
in that case, ct::Assert is only one character longer than ct_assert.
(Yes, I realize it doesn't really help in the case of assertions, since we
need a macro).
-Dave
----- Original Message -----
From: "John Maddock" <John_Maddock_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Sunday, July 23, 2000 7:39 AM
Subject: [boost] metactrl header [compile time asserts]
> Beman,
>
> Last time this was discussed, the best candidate that Steve and I came up
> with was something like the form given below, usage is very simple, and
> since it only generates a typedef, it can be used anywhere (class,
function
> or namespace scope) without generating any code or data:
>
> namespace boost{
>
> template <bool> struct compile_time_assert;
>
> template <> struct compile_time_assert<true>{};
>
> template<int> struct ct_assert_test{};
>
> }
>
> #define BOOST_CT_ASSERT( B ) typedef
> ::boost::ct_assert_test<sizeof(::boost::compile_time_assert< ( B ) >)>
> BOOST_JOIN(_boost_postulate_details_,__LINE__)
>
> #define BOOST_DO_JOIN( X, Y ) BOOST_DO_JOIN2(X,Y)
> #define BOOST_DO_JOIN2(X, Y) X ## Y
> #define BOOST_JOIN( X, Y ) BOOST_DO_JOIN( X, Y )
>
> // test cases, these should all compile, uncomment those tests that are
> currently commented out to see error messages:
>
> // Namespace scope
> BOOST_CT_ASSERT(sizeof(int) == 4);
> BOOST_CT_ASSERT(sizeof(char) == 1);
> //BOOST_CT_ASSERT(sizeof(int) == sizeof(char)); // will not compile
>
> // Function (block) scope
> void f()
> {
> BOOST_CT_ASSERT(sizeof(int) == 4);
> BOOST_CT_ASSERT(sizeof(char) == 1);
> //BOOST_CT_ASSERT(sizeof(int) == sizeof(char)); // should not compile
> }
>
> struct Bob
> {
> private: // can be in private, to avoid namespace pollution
> BOOST_CT_ASSERT(sizeof(int) == 4);
> BOOST_CT_ASSERT(sizeof(char) == 1);
> //BOOST_CT_ASSERT(sizeof(int) == sizeof(char)); // will not compile
> public:
>
> // Member function scope: provides access to member variables
> int x;
> char c;
> int f()
> {
> #ifndef _MSC_VER // broken sizeof in VC6
> BOOST_CT_ASSERT(sizeof(x) == 4);
> BOOST_CT_ASSERT(sizeof(c) == 1);
> #endif
> //BOOST_CT_ASSERT((sizeof(x) == sizeof(c))); // should not compile
> return x;
> }
> };
>
> - John.
>
> ------------------------------------------------------------------------
> Failed tests, classes skipped, forgotten locker combinations.
> Remember the good 'ol days
> http://click.egroups.com/1/7076/4/_/9351/_/964352399/
> ------------------------------------------------------------------------
>
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk