[cfe-dev] Determining whether Container<T>()==Container<T>() can compile?

Stephen Kelly steveire at gmail.com
Wed May 29 05:42:51 PDT 2013


Stephen Kelly wrote:
> I can't actually figure out how to use libcxx on linux, so the above is
> not tested.

I hacked around a bit with libcxxabi and tested the patch. The testcode now 
indeed gives the expected results for std::vector:


#include <iostream>
#include <vector>

template<typename T, typename U = bool>
struct HasEqualityComparison
{
  enum { value = false };
};

template<typename T>
struct HasEqualityComparison<T, decltype(std::declval<T&>() == 
std::declval<T&>())>
{
  enum { value = true };
};

struct A {

};

struct B {
  bool operator==(const B &other)
  {
    return true;
  }
};

int main()
{
  std::cout << HasEqualityComparison<A>::value << std::endl;
  std::cout << HasEqualityComparison<std::vector<A>>::value << std::endl;
  std::cout << HasEqualityComparison<B>::value << std::endl;
  std::cout << HasEqualityComparison<std::vector<B>>::value << std::endl;
  std::cout << HasEqualityComparison<std::vector<std::vector<A>>>::value << 
std::endl;
  std::cout << HasEqualityComparison<std::vector<std::vector<B>>>::value << 
std::endl;
  return 0;
}




$ ./a.out 
0
0
1
1
0
1


All that remains is figuring out how to get the stl implementations updated 
to use that for all containers, if possible.

Thanks,

Steve.





More information about the cfe-dev mailing list