template<typename T> classVectorSafety :public std::vector<T> { public: autooperator[](size_t i) const -> std::enable_if_t<std::is_trivially_default_constructible<T>::value, T> { if (i < this->size()) { returnthis->std::vector<T>::operator[](i); } else { // 可以根据需求抛出异常或返回一个特殊值 //throw std::out_of_range("VectorSafety index out of range"); LogErr("VectorSafety index out of range"); } // 返回默认构造的元素 returnT(); }
autooperator[](size_t i) -> std::enable_if_t<std::is_trivially_default_constructible<T>::value, const T&> { if (i < this->size()) { returnthis->std::vector<T>::operator[](i); } else { // 可以根据需求抛出异常或返回一个特殊值 //throw std::out_of_range("VectorSafety index out of range"); LogErr("VectorSafety index out of range"); }