Boost logo

Boost :

Subject: [boost] [cpp0x] Macro to support some C++0x syntax and functionality
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2010-04-04 12:33:02

Hello all,

Consider the following C++0x-like example (inspired by ConceptC++ and
C++ Contract Programming proposal n1962):

    // Example strangely uses pointers instead of iterators just to
have preconditions.
    template<std::CopyConstructible T>
        requires Addable<T>, Assignable<T> // Concepts.
        precondition { first; last; } // Pointers not null.
        postcondition (total) { total == result; } // Returning result.
    T sum(T* first, T* last, T result) {
        for (; first != last; ++first)
            result = result + *first;
        return result;

What do you think of the following macro that implementes this in ISO
standard C++ while keeping the above C++0x-like syntax by wrapping it
within a preprocessor sequence?

    (template)( (std::CopyConstructible)(T) )
        (requires)( (Addable<T>) (Assignable<T>) )
        (precondition)( (first) (last) )
        (postcondition)(total)( (total == result) )
    (T) (sum)( (T*)(first) (T*)(last) (T)(result) ) ({
        for (; first != last; ++first)
            result = result + *first;
        return result;
    }) )

The idea is that using the preprocessor I can parse the specified
signature-sequence so I have all the tokens I need to declare/define
the C++ function plus I can add the concepts (Boost.Concept),
contracts (Boost.Contract), named parameters (Boost.Parameter), etc.
Furthermore, the syntax of signature-sequence can be any valid
preprocessor sequence that uses words (but not symbols) for keywords
-- e.g., (requires) and (preconditions) are OK but (=) is not. This
way I can define the signature-sequence syntax to closely match the
syntax of C++0x (for concepts) and of other C++ standard proposals
(like n1962 for contracts).

DISCLAIMER: I think this is possible but I would need to try to
implement it in order to be 100% sure and to identify possible


P.S. I started considering this in reference to a previous email
thread "Re: [boost] [contract] Contract Programming Library":
On Sat, Feb 13, 2010 at 9:44 AM, Lorenzo Caminiti <lorcaminiti_at_[hidden]> wrote:
> On Fri, Feb 12, 2010 at 3:39 AM, Andrzej Krzemienski <akrzemi1_at_[hidden]> wrote:
>> I would be very interested in trying such a library. I have one
>> it is inevitable, etc., but.. read on. I know at least one library in
>> Boost that also spoils function declarations in order to provide
>> additional functionality: Concept Check library; they may be others
>> too (MPL?).
>> My suggestion is that if there are (or will be) libraries that require
>> spoiling function declarations, they should all provide the same
>> "spoiled" syntax. Otherwise I will be always asking "how do I declare
>> a function in this library?". It would be very convenient if Boost
>> provided one alternative function definition syntax that when used
>> would enable all its libraries to work.
> I agree. However, in the past I did look into harmonizing my library
> API with the ones of Boost.ConceptCheck and/or Boost.Parameter but it
> did not seem feasible... I will double check it.

Boost list run by bdawes at, gregod at, cpdaniel at, john at