diff --git a/src/mini_safe_array/mini_safe_array.cpp b/src/mini_safe_array/mini_safe_array.cpp new file mode 100644 index 0000000..31fdbd2 --- /dev/null +++ b/src/mini_safe_array/mini_safe_array.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using ll = long long; +#define NVF(v)#v," : ",(v) + +template +std::string fmt(Args&&...args){ + std::ostringstream oss; + (oss<<...<(args)); + return std::string(oss.str()); +} + +template +void print(Args&&...args){ + std::ostringstream oss; + (oss<<...<(args)); + std::cout<//size_t +class SafeArray{ + T arr[_size]; +public: + constexpr SafeArray()NDB_NOEXCEPT{}; + constexpr SafeArray(const std::initializer_list init)NDB_NOEXCEPT:arr{}{ + #ifndef NDEBUG + if(!std::is_constant_evaluated())[[unlikely]]{ + if(init.size()>_size){ + throw std::runtime_error(fmt("std::initializer _size error when constructing SafeArray\n", + "in ",__PRETTY_FUNCTION__," \n", + "initialize_list should < ",_size,"\n" + "but your initialize_list.size is ",init.size(),"\n")); + } + } + #endif + std::copy(init.begin(),init.end(),arr); + } + constexpr ll size()const noexcept{ + return size; + } + constexpr T& operator[](const ll index)NDB_NOEXCEPT{ + #ifndef NDEBUG + if(!std::is_constant_evaluated()){ + if(!(0<=index&&index<_size))[[unlikely]]{ + throw std::runtime_error(fmt("in ",__PRETTY_FUNCTION__," error!\n", + "0 <= index < ",_size,'\n', + "but your index is : ",index,"\n")); + } + } + #endif + return arr[index]; + } + constexpr const T& operator[](const ll index)const NDB_NOEXCEPT{ + #ifndef NDEBUG + if(!std::is_constant_evaluated()){ + if(!(0<=index&&index<_size))[[unlikely]]{ + throw std::runtime_error(fmt("in ",__PRETTY_FUNCTION__," error!\n", + "0 <= index < ",_size,'\n', + "but your index is :",index,"\n")); + } + } + #endif + return arr[index]; + } +}; + +int main(){ + // const int *const p = new int; + // p = new int; + + constexpr SafeArray arr{1,2,3,4,5}; + SafeArray arr1{0,1,2,3,4}; + arr1[4]=5; + arr[-1]; + print(NVF(arr[4]),'\n', + NVF(arr[5]),'\n'); +} \ No newline at end of file