fork download
  1. #include <iostream>
  2.  
  3. template < std::size_t POS > struct get_array_element
  4. {
  5. template < typename T >
  6. auto operator() ( T&& t ) const -> decltype( *( std::forward<T>(t) ).array )
  7. { return std::forward<T>(t).array[POS] ; }
  8. };
  9.  
  10. struct vertex { float array [5] ; } ;
  11.  
  12. constexpr auto x = get_array_element<0>() ;
  13. constexpr auto y = get_array_element<1>() ;
  14. constexpr auto z = get_array_element<2>() ;
  15. constexpr auto u = get_array_element<3>() ;
  16. constexpr auto v = get_array_element<4>() ;
  17.  
  18. int main ()
  19. {
  20. vertex vert { { 1.2, 3.4, 5.6, 7.8, 9.1 } } ;
  21. y(vert) += 0.5 ;
  22. std::cout << x(vert) << ", " << y(vert) << ", " << z(vert)
  23. << ", " << u(vert) << ", " << v(vert) << '\n' ;
  24. }
  25.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
1.2, 3.9, 5.6, 7.8, 9.1