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. 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 ConstIterator begin() const {
  63. return Buff.begin();
  64. }
  65. constexpr Iterator end() {
  66. return Buff.begin()+C;
  67. }
  68. constexpr ConstIterator end() const {
  69. return Buff.begin()+C;
  70. }
  71. };
  72.  
  73. #include <iostream>
  74. #include <cstdint>
  75.  
  76. //#include "ConstexprVector.h"
  77.  
  78. typedef ConstexprVector<std::uint64_t, 128> DType;
  79.  
  80. constexpr std::uint64_t Calc(const DType& D) {
  81. std::uint64_t C = 0;
  82. for (const DType::ValueType& o : D) {
  83. C += o;
  84. }
  85.  
  86. return C;
  87. }
  88.  
  89. int main() {
  90. //constexpr std::initializer_list<DType::ValueType> L{ 1,2,3,4,5,6,7,8,9 };
  91. constexpr DType D{ };
  92. constexpr std::uint64_t R = Calc(D);
  93.  
  94. std::cout << R << std::endl;
  95.  
  96. return true;
  97. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:92:34:   in constexpr expansion of ‘Calc(D)’
prog.cpp:82:35: error: ‘constexpr ConstexprVector<T, N>::ConstIterator ConstexprVector<T, N>::begin() const [with T = long unsigned int; long unsigned int N = 128ul; ConstexprVector<T, N>::ConstIterator = const long unsigned int*]’ called in a constant expression
  for (const DType::ValueType& o : D) {
                                   ^
prog.cpp:62:26: note: ‘constexpr ConstexprVector<T, N>::ConstIterator ConstexprVector<T, N>::begin() const [with T = long unsigned int; long unsigned int N = 128ul; ConstexprVector<T, N>::ConstIterator = const long unsigned int*]’ is not usable as a constexpr function because:
  constexpr ConstIterator begin() const {
                          ^~~~~
prog.cpp:63:21: error: call to non-constexpr function ‘const value_type* std::array<_Tp, _Nm>::begin() const [with _Tp = long unsigned int; long unsigned int _Nm = 128ul; std::array<_Tp, _Nm>::const_iterator = const long unsigned int*; std::array<_Tp, _Nm>::value_type = long unsigned int]’
   return Buff.begin();
                     ^
stdout
Standard output is empty