fork download
  1. //#pragma once
  2. #include <array>
  3. #include <initializer_list>
  4. #include <algorithm>
  5. template<class T,std::size_t N>
  6. class ConstexprVector {
  7. std::array<T, N> Buff{};
  8. std::size_t C = 0;
  9. public:
  10.  
  11. typedef T ValueType;
  12. typedef typename std::array<T, N>::iterator Iterator;
  13. typedef typename std::array<T, N>::const_iterator ConstIterator;
  14. constexpr ConstexprVector(){};
  15. constexpr explicit ConstexprVector(const std::initializer_list<T>& L){
  16. std::size_t V = 0;
  17. auto it1 = L.begin();
  18. auto it2 = Buff.begin();
  19. for (it1 ,it2 ; it1 != L.end() && it2 != Buff.end(); it1++, it2++) {
  20. *it2 = *it1;
  21. V++;
  22. }
  23. C = V;
  24. }
  25. constexpr std::size_t Size() const{
  26. return C;
  27. }
  28. constexpr std::size_t MaxSize() const {
  29. return N;
  30. }
  31. constexpr bool PushBack(const T& In) {
  32. if (C >= Buff.size()) return false;
  33. Buff[C] = In;
  34. C++;
  35. return true;
  36. }
  37. /** /
  38. template<class... A>
  39. constexpr bool EmplaceBack(const A& ...In) {
  40. if (C >= Buff.size()) return false;
  41. Buff[C] = T(In...);
  42. C++;
  43. return true;
  44. }
  45. /**/
  46. constexpr bool PopBack() {
  47. if (C == 0)return false;
  48. //Buff[C]~T();
  49. C--;
  50. return true;
  51. }
  52.  
  53. constexpr bool Clear() {
  54. while (PopBack());
  55. return true;
  56. }
  57.  
  58. constexpr Iterator begin() {
  59. return Buff.begin();
  60. }
  61.  
  62. constexpr Iterator begin() const {
  63. return Buff.begin();
  64. }
  65. constexpr Iterator end() {
  66. return Buff.begin()+C;
  67. }
  68. constexpr Iterator end() const {
  69. return Buff.begin()+C;
  70. }
  71. constexpr T& operator[](std::size_t idx) {
  72. int A = 0;
  73. if (Size() <= idx) A = 1/A;//kill?
  74.  
  75. return Buff[idx];
  76. }
  77. constexpr const T& operator[](std::size_t idx) const {
  78. int A = 0;
  79. if (Size() <= idx) A = 1/A;//kill?
  80.  
  81. return Buff[idx];
  82. }
  83. };
  84.  
  85.  
  86. #include <iostream>
  87. #include <cstdint>
  88.  
  89. //#include "ConstexprVector.h"
  90.  
  91. typedef ConstexprVector<std::uint64_t, 128> DType;
  92.  
  93. constexpr std::uint64_t Calc(const DType& D) {
  94. std::uint64_t C = 0;
  95. for (std::size_t i = 0; i < D.Size();i++) {
  96. C += D[i];
  97. }
  98.  
  99. return C;
  100. }
  101.  
  102. int main() {
  103. constexpr DType D{ 1,2,3,4,5 };
  104. constexpr std::uint64_t R = Calc(D);
  105.  
  106. std::cout << R << std::endl;
  107.  
  108. return true;
  109. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:103: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:15: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:18:25: error: call to non-constexpr function ‘std::array<_Tp, _Nm>::value_type* std::array<_Tp, _Nm>::begin() [with _Tp = long unsigned int; long unsigned int _Nm = 128ul; std::array<_Tp, _Nm>::iterator = long unsigned int*; std::array<_Tp, _Nm>::value_type = long unsigned int]’
   auto it2 = Buff.begin();
                         ^
stdout
Standard output is empty