fork(3) download
  1. #include <iostream>
  2.  
  3. template<typename T, size_t N>
  4. constexpr std::size_t arraySize(T (&)[N]) noexcept{
  5. return N;
  6. }
  7.  
  8.  
  9. template<typename T>
  10. class V{
  11. public:
  12. using type = V<T>;
  13. constexpr V(T x, T y) noexcept : x(x), y(y) {}
  14. friend constexpr type operator+(const type& lhs, const type& rhs) noexcept{
  15. return std::move ( type(lhs.x + rhs.x, lhs.y + rhs.y) );
  16. }
  17. constexpr T lengthSqare () noexcept {return x*x+y*y;};
  18. T x,y;
  19. };
  20.  
  21. int main(int argc, const char * argv[]) {
  22. char a1[(V<int>(1,2) + V<int>(3,5)).x];
  23. char a2[V<int>(2,2).lengthSqare()];
  24. std::cout<<"Some compile time vec+ "<<arraySize(a1)<<std::endl;
  25. std::cout<<"And compile time lenght^2 is: "<<arraySize(a2)<<std::endl;
  26. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
Some compile time vec+ 4
And compile time lenght^2 is: 8