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

Stephen Kelly steveire at gmail.com
Wed May 29 05:23:04 PDT 2013


On 05/29/2013 10:12 AM, Mailing List Email wrote:
>
> The problem essentially comes from that there exists an operator == 
> that compares two vectors, hence decltype(a == b) would succeed, 
> because decltype does not evaluate its arguments.
> Instead, the problem happens inside the operator == when it tries to 
> compare the elements.

Yes, I think that's right. Using the type of begin() is a bit of a hack 
because it's not really typesafe. I'd prefer the stdlib container 
implementations to provide the correct result instead with patches 
something like

diff --git a/include/vector b/include/vector
index e04c267..41bd687 100644
--- a/include/vector
+++ b/include/vector
@@ -3168,8 +3168,9 @@ struct _LIBCPP_TYPE_VIS hash<vector<bool, 
_Allocator> >

  template <class _Tp, class _Allocator>
  _LIBCPP_INLINE_VISIBILITY inline
-bool
+auto
  operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, 
_Allocator>& __y)
+->decltype(std::declval<typename vector<_Tp, 
_Allocator>::value_type&>() == std::declval<typename vector<_Tp, 
_Allocator>::value_type&>())
  {
      const typename vector<_Tp, _Allocator>::size_type __sz = __x.size();
      return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), 
__y.begin());



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

Thanks,

Steve.




More information about the cfe-dev mailing list