https://ideone.com/d05dJ9
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
#define MAX_SIZE 100
template <class T>
// template <class T, size_t C=MAX_SIZE>
class SizeLimitedAllocator {
public:
typedef T value_type;
using traits = allocator_traits<allocator<T>>;
T *allocate(size_t n) {
if (n > MAX_SIZE) throw bad_alloc();
// if (n > C) throw bad_alloc();
return traits::allocate(_allocator, n);
}
void deallocate(T *p, size_t n) {
traits::deallocate(_allocator, p, n);
}
private:
allocator<T> _allocator;
};
int main()
{
vector<char,SizeLimitedAllocator<char>> vec(MAX_SIZE);
try { vec.resize(MAX_SIZE+1); }
catch (bad_alloc& e) { cerr << e.what() << endl; }
return 0;
}
このコードをコメント側に変更してコンパイルするとこけるんだけど、なんで?