fork download
  1. #include <iostream>
  2. #include <initializer_list>
  3. #include <iterator>
  4. #include <array>
  5. #include <vector>
  6. #include <sstream>
  7.  
  8. struct A
  9. {
  10. template < typename ITERATOR > A( ITERATOR begin, ITERATOR end )
  11. {
  12. std::cout << "A::constructor - " ;
  13. for( ; begin != end ; ++begin ) std::cout << *begin << ' ' ;
  14. std::cout << '\n' ;
  15. }
  16.  
  17. template < typename T >
  18. A( std::initializer_list<T> range ) : A( std::begin(range), std::end(range) ) {}
  19.  
  20. template < typename RANGE >
  21. A( const RANGE& range, decltype( std::begin(range) )* = nullptr )
  22. : A( std::begin(range), std::end(range) ) {}
  23. };
  24.  
  25. int main()
  26. {
  27. double carray[] { 10.1, 20.2, 30.3, 40.4 } ;
  28. A a1(carray) ; // construct from C-array (range)
  29.  
  30. std::array<long,5> array { { 12, 13, 14 } } ;
  31. A a2(array) ; // from std::array (range)
  32.  
  33. std::vector<short> vector { 91, 92, 93 } ;
  34. A a3(vector) ; // from sequence container (range)
  35.  
  36. A a4( { 1, 2, 3, 4, 5 } ) ; // from initializer list
  37.  
  38. std::istringstream stm( "50 60 70 80" ) ;
  39. // from pair of iterators (
  40. A a5( (std::istream_iterator<int>(stm)), std::istream_iterator<int>() ) ;
  41. }
  42.  
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
A::constructor - 10.1 20.2 30.3 40.4 
A::constructor - 12 13 14 0 0 
A::constructor - 91 92 93 
A::constructor - 1 2 3 4 5 
A::constructor - 50 60 70 80