Boost logo

Boost :

Subject: Re: [boost] [contract] syntax redesign
From: lcaminiti (lorcaminiti_at_[hidden])
Date: 2011-06-01 10:01:59


Matt Calabrese wrote:
>
> On Tue, May 31, 2011 at 6:35 PM, lcaminiti <lorcaminiti_at_[hidden]>
> wrote:
>>
>> For example? (Just so I understand this case correctly.)
>>
>
> //////////
> CONTRACT_FUNCTION(
> public template( class T, class Alloc )
> void (push_back)( (BOOST_IDENTITY_TYPE((std::vector< T, Alloc >&)))
> vector_,
> (const T&) element ) {
> vector_.push_back(element);
> }
> //////////
>
> In the above case, since the first parameter type actually ends up being
> the
> result of a metafunction invocation, you can't simply call the function by
> doing this_.push_back( an_instance_of_std_vector, 4 ). T and Alloc can no
> longer be deduced.
>

Yes, I see:

#include &lt;boost/type_traits.hpp&gt;
#include <memory>
#include <vector>
#include <iostream>

#define IDENTITY_TYPE(paren_type) \
    boost::function_traits< void paren_type >::arg1_type

template< typename T, class Alloc >
void push_back(
        // comma in type is now wrapped in parenthesis (ok for macros)
        typename IDENTITY_TYPE((std::vector< T, Alloc >&)) vector,
        const T& element ) {
  vector.push_back(element);
}

int main() {
    std::vector<int> v;
// push_back(v, -1); // Error: Can't deduce vector type via
IDENTITY_TYPE.
    push_back< int, std::allocator<int> >(v, -1); // OK but no type
deduction :((
    std::cout << v[0] << std::endl;

    return 0;
}

Questions:
1) How bad is this? I am afraid that giving up this kind of automatic type
deduction makes generic programming less convenient... am I right? ( I'm not
a generic programmer ;) )
2) Does anyone know of a better way to handle commas within macro parameter
types but without using variadics?

Note: Because of this I will try to provide a different implementation of
IDENTITY_TYPE when variadics are supported (however, to properly handle the
types with commas I think that not just IDENTITY_TYPE but also the CONTRACT
macros need to modified to be aware that the type if a pp tuple). The same
applies to Boost.Local macros.

Thanks a lot!
--Lorenzo

--
View this message in context: http://boost.2283326.n4.nabble.com/contract-syntax-redesign-tp3563993p3565915.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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