|
Boost : |
From: Beman Dawes (beman_at_[hidden])
Date: 2000-11-24 20:20:02
At 01:03 PM 11/21/2000 -0500, Jeremy Siek wrote:
>On Mon, 20 Nov 2000, Matthew Austern wrote:
>> - I keep hoping that there might be a way to eliminate the
>> use of macros. Token pasting, in particular, is distasteful.
>
>Here's my first stab at a non-macro approach:
>
>First, for the function requires we can just use a function
>template like this:
>
>template <class Concept>
>void function_requires()
>{
> void (Concept::*x)() = BOOST_FPTR Concept::constraints;
> ignore_unused_variable_warning(x);
>}
>
>And then use explicit template arguments to invoke it:
>
>boost::function_requires< DefaultConstructibleConcept<foo> >();
That's fine.
>Second, for class requires we can create a class template
>like this:
>
>template <class Concept>
>struct class_requires
>{
> typedef void (Concept::* function_pointer)();
>
> template <function_pointer Fptr>
> struct dummy_struct { };
>
> typedef dummy_struct< BOOST_FPTR Concept::constraints > check;
>};
>
>and then invoke it like this:
>
>class class_requires_test
>{
> typedef class_requires< EqualityComparableConcept<int> >::check req1;
That's not so good. The "typedef", "::check", and "req1" detract from the
clarity of the code. I worry developers will shy away from using concept
checks because of the ugly code.
Another worry: in testing an early compile time asserts proposal which used
a similar typedef technique, I found a number of slight coding mistakes
would compile OK, but not actually do the indicated check. What happens if
a user codes any of these mistakes (where X is a UDT that isn't Equality
Comparable)?
class_requires< EqualityComparableConcept<X> >::check req1;
class_requires< EqualityComparableConcept<X> >::check;
class_requires< EqualityComparableConcept<X> > req2;
class_requires< EqualityComparableConcept<X> >;
typedef class_requires< EqualityComparableConcept<X> > req3;
typedef class_requires< EqualityComparableConcept<X> >;
If they fail to compiler, fine, but if they compile without error, and fail
to detect the error, I would be concerned. If they did error check OK, but
generated code because the typedef was missing, that would also be a
concern. Could you give them a try?
While the macros were, well, macros, they were also very clear, and seemed
hard to misuse IMO.
--Beman
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk