Boost logo

Boost :

Subject: Re: [boost] [local] Simplifying the parenthesized syntax
From: John Bytheway (jbytheway+boost_at_[hidden])
Date: 2011-02-10 16:13:12


On 10/02/11 15:59, Lorenzo Caminiti wrote:
> On Mon, Feb 7, 2011 at 11:08 AM, Lorenzo Caminiti <lorcaminiti_at_[hidden]> wrote:
>> On Sun, Feb 6, 2011 at 10:26 AM, Vicente Botet <vicente.botet_at_[hidden]> wrote:
>>> The fact that you can not use variadic macros at work doesn't means that
>>> your library can not provide in addition variadic macros on compilers
>>> supporting them. I have no idea the work that this suppose, but if the
>>> interface could be more appealing your library will have much more people
>>> interested in.
>>>
>>> Have you an idea of how the interface could be simplified if variadic macros
>>> were used?
>
> Hello all,
>
> Based on some prototyping that I have done so far, I think I can
> simplify the local function macro syntax as follows using C99
> variadics:
>
> void BOOST_LOCAL_FUNCTION_PARAMS(int x, double y, // [1]
> bind a, bind& b, const bind c, const bind& d) {
> ...
> } BOOST_LOCLA_FUNCTION_NAME(f)
> f(1, 1.23);
>
> ** Note that there are NO extra parenthesis!! :-)) ** What do you
> think? Is this simple enough?

Amazing!

(I might argue that the parentheses around "f" are extra, but that would
be churlish ;))

<snip>

> The above macros have the important advantage of using NO extra
> parenthesis on C99 but they also have the following (small?)
> limitations:
>
> 1) They do not allow the local function to recursively call itself
> (because the function name is not specified until after the body is
> programmed so it is not available within the body for recursive
> calls).
> 2) They always require the use of Boost.Typeof to deduce the function
> result type (the library already requires Boost.Typeof for variable
> binding anyways).

You shouldn't need to use Typeof if you extract the return type in the
manner Steven Watanabe suggested:

#include <boost/mpl/assert.hpp>
#include <boost/type_traits/function_traits.hpp>
#include <boost/type_traits/is_same.hpp>

#define EXTRACT_TYPE_BEFORE \
  deduce_result(); \
  typedef boost::function_traits< \
    typeof(deduce_result)>::result_type result_type;

int main()
{
  void EXTRACT_TYPE_BEFORE

  BOOST_MPL_ASSERT((boost::is_same<result_type, void>));
}

It even gives a (relatively) nice error when the user forgets the return
type (on gcc 4.4):

‘deduce_result’ was not declared in this scope

I share your hope that this syntax is enough to satisfy most of the
objectors! Do check how they respond when misused, though; a little
effort to improve error messages might go a long way.

John


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk