fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. #include <boost/range.hpp>
  5.  
  6. using namespace std;
  7.  
  8. template<typename...> struct void_ { using type = void; };
  9. template<typename... T> using Void = typename void_<T...>::type;
  10.  
  11. template<typename... T> using BoostRangeValue = typename boost::range_value<T...>::type;
  12.  
  13. //////////////////
  14. template<typename T, typename Sfinae = void>
  15. struct range_value {};
  16.  
  17. template<typename T>
  18. struct range_value<T, Void<BoostRangeValue<T>>>: BoostRangeValue<T> {};
  19.  
  20. template<typename... T> using RangeValue = typename range_value<T...>::type;
  21. //////////////////
  22.  
  23. template <typename T, typename C>
  24. typename enable_if<std::is_same<T, RangeValue<C>>::value, C>::type
  25. concat(C c, T v)
  26. {
  27. c.insert(end(c), v);
  28. return c;
  29. }
  30.  
  31. template <typename R, typename C>
  32. typename enable_if<
  33. std::is_same< RangeValue<R>, RangeValue<C>
  34. >::value, C>::type
  35. concat(C c, R const& r)
  36. {
  37. c.insert(end(c), begin(r), end(r));
  38. return c;
  39. }
  40.  
  41. //////////////////
  42. // THIS IS CHEATING...
  43. // template<> struct range_value<int, void> {};
  44.  
  45. //////////////////
  46. int main()
  47. {
  48. auto v = concat(vector<int> { 1,2,3 }, list<int> { 4,5,6 });
  49. for(auto i : v)
  50. std::cout << i << " ";
  51.  
  52. // does not SFINAE?
  53. v = concat(vector<int> { 1,2,3 }, 42);
  54. }
  55.  
  56.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:27: fatal error: boost/range.hpp: No such file or directory
compilation terminated.
stdout
Standard output is empty