Boost logo

Boost :

Subject: Re: [boost] [type_traits] has_equal_to< std::vector<T> > always true_
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2011-10-20 11:09:42

On Thu, Oct 20, 2011 at 7:55 AM, lcaminiti <lorcaminiti_at_[hidden]> wrote:

> Hello all,
> has_equal_to< std::vector<T> > always returns true_ because == is defined for std::vector<T> even when it is not defined for T. However, then an attempt to compare for equality std::vector<T> will generate a compiler error in this case because there is no == for T.
> For example:
> #include <boost/type_traits/has_equal_to.hpp>
> #include <vector>
> #include <iostream>
> struct myvalue {};
> //bool operator== ( myvalue const& l, myvalue const& r ) { return false; } // (1)
> int main ( )
> {
> typedef std::vector<myvalue> myvector;
> std::cout << boost::has_equal_to<myvector>::value << std::endl; // always returns true
> myvector v(1), w(1);
> std::cout << (v == w) << std::endl; // compiler error because (1) is not defined
> return 0;
> }
> Shall has_equal_to< std::vector<T> > be smarter and return false_ if T does not have == ? (If not, this case should at least be documented.)
I'd venture to say this is a deficiency in std::vector's implementation of
operator==, which ideally should SFINAE-out bindings of T which do not have
an operator==. You get the same sort of problem if you have a class with an
unbounded conversion constructor template:

struct X
    template< class T >
    X(T x)
    { /*...*/ }

Here, I believe, boost::is_convertible< Y, X > will evaluate to true, and
some bindings of Y to T may result in a compiler error (depending on what's
in /*...*/). I *seem* to remember this was a problem with boost::variant,
for example, but even if it wasn't, you can imagine how it could be (the
template parameter in the constructor should only be bindable to one of the
value_types of the variant).

In my opinion, the upshot is that std::vector's operator== is almost
certainly not a special case, so I don't know know if it's worth
specializing has_equal_to on it or not. Maybe, if it's consistently done
for all problematic std-defined operators?

P.S. Are the strange HTML tags for < and > gone?
Yes, but the above quoted message only appeared when I clicked on the link:

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