fork(1) download
  1. #include <array>
  2. //#include <initializer_list>
  3. #include <algorithm>
  4. template<class T,std::size_t N>
  5. class ConstexprVector {
  6. T Buff[N]{};
  7. std::size_t C = 0;
  8. public:
  9.  
  10. typedef T ValueType;
  11. typedef typename T* Iterator;
  12. typedef typename T const* ConstIterator;
  13. constexpr ConstexprVector(){};
  14. constexpr explicit ConstexprVector(const std::initializer_list<T>& L) {
  15. std::size_t V = 0;
  16. auto it1 = L.begin();
  17. auto it2 = &Buff[0];
  18. for (it1, it2; L.end() != it1&&it2 != &Buff[N]; it1++, it2++) {
  19. *it2 = *it1;
  20. V++;
  21. }
  22. C = V;
  23. }
  24. template<class Iter>
  25. constexpr explicit ConstexprVector(Iter Beg,Iter End) {
  26. std::size_t V = 0;
  27. auto it1 = Beg;
  28. auto it2 = &Buff[0];
  29. for (it1, it2; End != it1&&it2 != &Buff[N]; it1++, it2++) {
  30. *it2 = *it1;
  31. V++;
  32. }
  33. C = V;
  34. }
  35. constexpr std::size_t Size() const{
  36. return C;
  37. }
  38. constexpr std::size_t MaxSize() const {
  39. return N;
  40. }
  41. constexpr bool PushBack(const T& In) {
  42. if (C >= N) return false;
  43. Buff[C] = In;
  44. C++;
  45. return true;
  46. }
  47. /** /
  48. template<class... A>
  49. constexpr bool EmplaceBack(const A& ...In) {
  50. if (C >= Buff.size()) return false;
  51. Buff[C] = T(In...);
  52. C++;
  53. return true;
  54. }
  55. /**/
  56. constexpr bool PopBack() {
  57. if (C == 0)return false;
  58. //Buff[C]~T();
  59. C--;
  60. return true;
  61. }
  62.  
  63. constexpr bool Clear() {
  64. while (PopBack());
  65. return true;
  66. }
  67.  
  68. constexpr Iterator begin() {
  69. return Buff;
  70. }
  71.  
  72. constexpr ConstIterator begin() const {
  73. return &Buff[0];
  74. }
  75. constexpr Iterator end() {
  76. return Buff+C;
  77. }
  78. constexpr ConstIterator end() const {
  79. return Buff+C;
  80. }
  81. constexpr T& operator[](std::size_t idx) {
  82. int A = 0;
  83. if (Size() <= idx) A = 1/A;//kill?
  84.  
  85. return Buff[idx];
  86. }
  87. constexpr const T& operator[](std::size_t idx) const {
  88. int A = 0;
  89. if (Size() <= idx) A = 1/A;//kill?
  90.  
  91. return Buff[idx];
  92. }
  93. };
  94.  
  95. #include <iostream>
  96. #include <cstdint>
  97.  
  98. //#include "ConstexprVector.h"
  99.  
  100. typedef ConstexprVector<std::uint64_t, 128> DType;
  101. constexpr std::uint64_t Calc2(const DType& D) {
  102. std::uint64_t C = 0;
  103. for (auto& o:D) {
  104. C += o;
  105. }
  106.  
  107. return C;
  108. }
  109. constexpr std::uint64_t Calc(const DType& D) {
  110. std::uint64_t C = 0;
  111. for (std::size_t i = 0; i < D.Size();i++) {
  112. C += D[i];
  113. }
  114.  
  115. return C;
  116. }
  117.  
  118. constexpr DType Add(const DType& D, DType::ValueType V) {
  119. DType R(D.begin(), D.end());
  120. R.PushBack(V);
  121.  
  122. return R;
  123.  
  124. }
  125. int main() {
  126. constexpr DType D{ 1,2,3,4,5 };
  127. constexpr std::uint64_t R = Calc(D);
  128. constexpr std::uint64_t R2 = Calc2(D);
  129. constexpr DType D2= Add(D, 6);
  130. constexpr std::uint64_t R3 = Calc(D2);
  131. std::cout << R << std::endl;
  132. std::cout << R3 << std::endl;
  133. return true;
  134. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:11:19: error: expected nested-name-specifier before ‘T’
  typedef typename T* Iterator;
                   ^
prog.cpp:11:19: error: expected ‘;’ at end of member declaration
prog.cpp:11:19: error: declaration of ‘typedef int ConstexprVector<T, N>::T’ shadows template parameter
prog.cpp:4:10: note: template parameter ‘T’ declared here
 template<class T,std::size_t N>
          ^~~~~
prog.cpp:11:22: error: ISO C++ forbids declaration of ‘Iterator’ with no type [-fpermissive]
  typedef typename T* Iterator;
                      ^~~~~~~~
prog.cpp:12:19: error: expected nested-name-specifier before ‘T’
  typedef typename T const* ConstIterator;
                   ^
prog.cpp:12:19: error: expected ‘;’ at end of member declaration
prog.cpp:12:19: error: declaration of ‘typedef int ConstexprVector<T, N>::T’ shadows template parameter
prog.cpp:4:10: note: template parameter ‘T’ declared here
 template<class T,std::size_t N>
          ^~~~~
prog.cpp:12:28: error: ISO C++ forbids declaration of ‘ConstIterator’ with no type [-fpermissive]
  typedef typename T const* ConstIterator;
                            ^~~~~~~~~~~~~
prog.cpp:68:12: error: ‘Iterator’ does not name a type
  constexpr Iterator begin() {
            ^~~~~~~~
prog.cpp:72:12: error: ‘ConstIterator’ does not name a type
  constexpr ConstIterator begin() const {
            ^~~~~~~~~~~~~
prog.cpp:75:12: error: ‘Iterator’ does not name a type
  constexpr Iterator end() {
            ^~~~~~~~
prog.cpp:78:12: error: ‘ConstIterator’ does not name a type
  constexpr ConstIterator end() const {
            ^~~~~~~~~~~~~
prog.cpp: In function ‘constexpr uint64_t Calc2(const DType&)’:
prog.cpp:103:15: error: ‘begin’ was not declared in this scope
  for (auto& o:D) {
               ^
prog.cpp:103:15: note: suggested alternative:
In file included from /usr/include/c++/6/string:51:0,
                 from /usr/include/c++/6/stdexcept:39,
                 from /usr/include/c++/6/array:39,
                 from prog.cpp:1:
/usr/include/c++/6/bits/range_access.h:105:37: note:   ‘std::begin’
   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&);
                                     ^~~~~
prog.cpp:103:15: error: ‘end’ was not declared in this scope
  for (auto& o:D) {
               ^
prog.cpp:103:15: note: suggested alternative:
In file included from /usr/include/c++/6/string:51:0,
                 from /usr/include/c++/6/stdexcept:39,
                 from /usr/include/c++/6/array:39,
                 from prog.cpp:1:
/usr/include/c++/6/bits/range_access.h:107:37: note:   ‘std::end’
   template<typename _Tp> const _Tp* end(const valarray<_Tp>&);
                                     ^~~
prog.cpp: In function ‘constexpr DType Add(const DType&, ConstexprVector<long unsigned int, 128ul>::ValueType)’:
prog.cpp:119:12: error: ‘const DType {aka const class ConstexprVector<long unsigned int, 128ul>}’ has no member named ‘begin’
  DType R(D.begin(), D.end());
            ^~~~~
prog.cpp:119:23: error: ‘const DType {aka const class ConstexprVector<long unsigned int, 128ul>}’ has no member named ‘end’
  DType R(D.begin(), D.end());
                       ^~~
prog.cpp: In function ‘int main()’:
prog.cpp:126:31: error: ‘constexpr ConstexprVector<T, N>::ConstexprVector(const std::initializer_list<_Tp>&) [with T = long unsigned int; long unsigned int N = 128ul]’ called in a constant expression
  constexpr DType D{ 1,2,3,4,5 };
                               ^
prog.cpp:14:21: note: ‘constexpr ConstexprVector<T, N>::ConstexprVector(const std::initializer_list<_Tp>&) [with T = long unsigned int; long unsigned int N = 128ul]’ is not usable as a constexpr function because:
  constexpr explicit ConstexprVector(const std::initializer_list<T>& L) {
                     ^~~~~~~~~~~~~~~
prog.cpp:14:21: error: member ‘ConstexprVector<long unsigned int, 128ul>::Iterator’ must be initialized by mem-initializer in ‘constexpr’ constructor
prog.cpp:11:22: note: declared here
  typedef typename T* Iterator;
                      ^~~~~~~~
prog.cpp:14:21: error: member ‘ConstexprVector<long unsigned int, 128ul>::ConstIterator’ must be initialized by mem-initializer in ‘constexpr’ constructor
  constexpr explicit ConstexprVector(const std::initializer_list<T>& L) {
                     ^~~~~~~~~~~~~~~
prog.cpp:12:28: note: declared here
  typedef typename T const* ConstIterator;
                            ^~~~~~~~~~~~~
prog.cpp:128:36: error: ‘constexpr uint64_t Calc2(const DType&)’ called in a constant expression
  constexpr std::uint64_t R2 = Calc2(D);
                               ~~~~~^~~
prog.cpp:129: confused by earlier errors, bailing out
stdout
Standard output is empty