Boost logo

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; }
>> 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

#include &lt;boost/type_traits/can_call_equal.hpp&gt;
#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 ) {
        using dummy::operator==;
        assert( back() == value );

    T const& back ( ) const { return v_.back(); }
    std::vector<T> v_;

struct val {
    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

What do you think?


View this message in context:
Sent from the Boost - Dev mailing list archive at

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