[algs] Implement implicit casting between compatible pair types
authorBehdad Esfahbod <behdad@behdad.org>
Thu, 9 May 2019 22:31:24 +0000 (15:31 -0700)
committerBehdad Esfahbod <behdad@behdad.org>
Thu, 9 May 2019 22:31:24 +0000 (15:31 -0700)
src/hb-algs.hh
src/test-algs.cc

index dbb0805..bbf748e 100644 (file)
@@ -217,6 +217,11 @@ struct hb_pair_t
   hb_pair_t (T1 a, T2 b) : first (a), second (b) {}
   hb_pair_t (const pair_t& o) : first (o.first), second (o.second) {}
 
+  template <typename Q1, typename Q2,
+           hb_enable_if (hb_is_convertible (T1, Q1) &&
+                         hb_is_convertible (T2, T2))>
+  operator hb_pair_t<Q1, Q2> () { return hb_pair_t<Q1, Q2> (first, second); }
+
   hb_pair_t<T1, T2> reverse () const
   { return hb_pair_t<T1, T2> (second, first); }
 
index d6f34d7..774414a 100644 (file)
@@ -73,5 +73,8 @@ main (int argc, char **argv)
   z = 3;
   assert (x == 3);
 
+  hb_pair_t<const int*, int> xp = hb_pair_t<int *, long> (nullptr, 0);
+  xp = hb_pair_t<int *, double> (nullptr, 1);
+
   return 0;
 }