|
Boost : |
Subject: [boost] [contract] Boost spoiled function declarations
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2010-05-17 15:46:49
Hello all,
I have been looking into other Boost libraries besides my
Boost.Contract that spoil function declarations using macros. I found
that both Boost.Parameter (approved) and Boost.Interface (under
construction) significantly spoil function declarations.
Boost.ConceptCheck also spoils function declarations but its REQUIRE
macro has a very simple syntax so I am not comparing it with
Boost.Contract.
See examples below: Is Boost.Contract parenthesized syntax really more
complex than Boost.Parameter or Boost.Interface?
Boost.Contract (under construction)
BOOST_CONTRACT_FUNCTION(
(inline) (double) (sqrt)( (double)(x) )
(precondition)( (x >= 0.0) )
(postcondition)(result)( (result == x * x) )
({
...
}) )
Note that the usual C++ syntax would have been essentially the same as
above but without the extra parenthesis `()`.
Boost.Parameter (approved)
BOOST_PARAMETER_FUNCTION(
(void),
depth_first_search,
tag,
(required (graph, *) )
(optional
(visitor, *, boost::dfs_visitor<>())
(root_vertex, *, *vertices(graph).first)
(index_map, *, get(boost::vertex_index,graph))
(in_out(color_map), *,
default_color_map(num_vertices(graph), index_map) )
)
)
{
...
}
Boost.Interface (under construction)
BOOST_INTERFACE_BEGIN(IShape)
BOOST_INTERFACE_CONST_FUNCTION0(GetX, int)
BOOST_INTERFACE_CONST_FUNCTION0(GetY, int)
BOOST_INTERFACE_CONST_FUNCTION0(GetArea, double)
BOOST_INTERFACE_FUNCTION2(SetXY, void, (int, x), (int, y))
BOOST_INTERFACE_FUNCTION2(OffSetXY, void, (int, x), (int, y))
BOOST_INTERFACE_CONST_FUNCTION0(GetName, const char*)
BOOST_INTERFACE_END(IShape)
1) While I recognize the ugliness of the Boost.Contract parenthesized
syntax, I think it is actually simpler than Boost.Parameter or
Boost.Interface syntax because it is essentially "the usual C++ syntax
with all the tokens wrapped within parenthesis".
2) It is possible to provide Boost.Contract with macros similar to
ones of Boost.Parameter.
However, Boost.Parameter only supports const/non-const members while
Boost.Contract also supports volatile, virtual, inline/extern,
auto/register, function templates, and everything else. If specific
macros were to be provided for all these different function traits
plus their combinations, Boost.Contract will have a *lot* of macros.
For example, just to support qv-qualifiers, Boost.Contract should
provide CONTRACT_FUNCTION(), CONTRACT_FUNCTION_CONST(),
CONTRACT_FUNCTION_VOLATILE(), CONTRACT_FUNCTION_CONST_VOLATILE(). Plus
all these qv-qualifier macros will have to be combined with inline,
virtual, etc -- the number of macros grows combinatorially in the
number of function traits!
Instead the parenthesized syntax allows you to specify or omit the
function traits directly in the function declarations so all the
traits can be combined as usual in C++ and with the use of one single
macro CONTRACT_FUNCTION().
Regards,
-- Lorenzo
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk