|
Boost : |
Subject: Re: [boost] [contract] extra type requirements for contracts
From: lcaminiti (lorcaminiti_at_[hidden])
Date: 2011-09-23 09:45:10
Brent Spillner wrote:
>
> On Thu, 22 Sep 2011 10:30:14 lcaminiti wrote:
>>I think this can be achieved with my previous "option 2" suggestion:
>>
>>namespace dummy {
>> template< typename L, typename R>
>> bool operator== ( L const&, R const& ) { return true; }
>>}
>>
>>postcondition(
>> using dummy::operator==,
>> back() == value
>>)
>
> This will fail if L has a private (or protected) operator==.
>
Let me understand your point a bit better. I think you mean something like
this:
#include <boost/type_traits/can_call_equal.hpp>
#include <vector>
#include <iostream>
#include <cassert>
namespace dummy {
template< typename L, typename R >
bool operator== ( L const& left, R const& right ) {
std::clog << "dummy ==\n";
return true;
}
}
template< typename T >
struct vect {
void push_back ( T const& value ) {
v_.push_back(value);
using dummy::operator==;
assert( back() == value );
}
T const& back ( ) const { return v_.back(); }
private:
std::vector<T> v_;
};
struct val {
private:
bool operator== ( val const& right ) const {
std::clog << "val ==\n";
return true;
}
};
int main ( ) {
vect<val> v;
v.push_back(val()); // error 1
std::cout << boost::can_call_equal<val>::value << std::endl; // error 2
return 0;
};
In this case T = val has an operator== which is private. This causes both
the methods (1) using dummy::operator== and (2) the traits requirement
can_call_equal<val> to fail :(
Ideally, can_call_equal<val>::value will return false instead of generating
a compiler-error when val::operator== is private or protected. A part from
such an (impossible?) improvement to can_call_equal, I have no solution for
this issue. I can document it and at the end the library users are
responsible to program contracts with assertion requirements that can be
checked within C++ (and can_call_equal<val> when val has a private
operator== cannot be checked so it's simply a ill-formed assertion
requirement).
What do you think?
--Lorenzo
-- View this message in context: http://boost.2283326.n4.nabble.com/boost-contract-extra-type-requirements-for-contracts-tp3824535p3836884.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