#include <array> //#include <initializer_list> #include <algorithm> template<class T,std::size_t N> class ConstexprVector { T Buff[N]{}; std::size_t C = 0; public: typedef T ValueType; typedef typename T* Iterator; typedef typename T const* ConstIterator; constexpr ConstexprVector(){}; constexpr explicit ConstexprVector(const std::initializer_list<T>& L) { std::size_t V = 0; auto it1 = L.begin(); auto it2 = &Buff[0]; for (it1, it2; L.end() != it1&&it2 != &Buff[N]; it1++, it2++) { *it2 = *it1; V++; } C = V; } template<class Iter> constexpr explicit ConstexprVector(Iter Beg,Iter End) { std::size_t V = 0; auto it1 = Beg; auto it2 = &Buff[0]; for (it1, it2; End != it1&&it2 != &Buff[N]; it1++, it2++) { *it2 = *it1; V++; } C = V; } constexpr std::size_t Size() const{ return C; } constexpr std::size_t MaxSize() const { return N; } constexpr bool PushBack(const T& In) { if (C >= N) return false; Buff[C] = In; C++; return true; } /** / template<class... A> constexpr bool EmplaceBack(const A& ...In) { if (C >= Buff.size()) return false; Buff[C] = T(In...); C++; return true; } /**/ constexpr bool PopBack() { if (C == 0)return false; //Buff[C]~T(); C--; return true; } constexpr bool Clear() { while (PopBack()); return true; } constexpr Iterator begin() { return Buff; } constexpr ConstIterator begin() const { return &Buff[0]; } constexpr Iterator end() { return Buff+C; } constexpr ConstIterator end() const { return Buff+C; } constexpr T& operator[](std::size_t idx) { int A = 0; if (Size() <= idx) A = 1/A;//kill? return Buff[idx]; } constexpr const T& operator[](std::size_t idx) const { int A = 0; if (Size() <= idx) A = 1/A;//kill? return Buff[idx]; } }; #include <iostream> #include <cstdint> //#include "ConstexprVector.h" typedef ConstexprVector<std::uint64_t, 128> DType; constexpr std::uint64_t Calc2(const DType& D) { std::uint64_t C = 0; for (auto& o:D) { C += o; } return C; } constexpr std::uint64_t Calc(const DType& D) { std::uint64_t C = 0; for (std::size_t i = 0; i < D.Size();i++) { C += D[i]; } return C; } constexpr DType Add(const DType& D, DType::ValueType V) { DType R(D.begin(), D.end()); R.PushBack(V); return R; } int main() { constexpr DType D{ 1,2,3,4,5 }; constexpr std::uint64_t R = Calc(D); constexpr std::uint64_t R2 = Calc2(D); constexpr DType D2= Add(D, 6); constexpr std::uint64_t R3 = Calc(D2); std::cout << R << std::endl; std::cout << R3 << std::endl; return true; }
Standard input is empty
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
Standard output is empty