我正在Clion中实现一个异常安全的Stack数据结构及其通用功能,但不幸的是,当我运行该示例时,它启动了,但没有给出任何结果。。。比如挂着不回应。有什么解决问题的想法吗?
#include <cstddef>
#include <algorithm>
#include <cassert>
#include <stdexcept>
#include <initializer_list>
namespace my
{
template<typename T>
void destroy(T* p_ptr) {
p_ptr->~T();
}
template<typename FwdIt>
void destroy(FwdIt begin, FwdIt end) {
while(begin != end) {
destroy(begin);
}
++begin;
}
template<typename T>
class StackImpl {
public:
using value_type = T;
using reference = T&;
using const_reference = const T&;
using rval_reference = T&&;
using pointer_type = T*;
using const_pointer_type = const T*;
using size_type = std::size_t;
public:
StackImpl(size_type capacity = 0) : m_ptr {static_cast<pointer_type>
( !capacity ?
nullptr :
operator new(sizeof(size_type) * capacity))},
m_capacity{capacity},
m_size{0}
{}
~StackImpl() {
my::destroy(m_ptr, m_ptr + m_capacity);
operator delete(m_ptr);
}
public:
pointer_type m_ptr;
size_type m_capacity;
size_type m_size;
private:
StackImpl(const StackImpl<value_type>&) = delete;
StackImpl& operator=(const StackImpl<value_type>&) = delete;
};
}
int main(int argc, char **argv) {
my::StackImpl<int> myStack(10);
}
它挂在析构函数中。循环错误,条件begin != end
永远是true
。
template<typename FwdIt>
void destroy(FwdIt begin, FwdIt end) {
while(begin != end) {
destroy(begin);
}
++begin;
}
必须是
template<typename FwdIt>
void destroy(FwdIt begin, FwdIt end) {
while(begin != end) {
destroy(begin);
++begin;
}
}