我有以下课程:
template <typename Type = void> class AlignedMemory { public: AlignedMemory(size_t alignment,size_t size) : memptr_(0) { int iret(posix_memalign((void **)&memptr_,alignment,size)); if (iret) throw system_error("posix_memalign"); } virtual ~AlignedMemory() { free(memptr_); } operator Type *() const { return memptr_; } Type *operator->() const { return memptr_; } //operator Type &() { return *memptr_; } //Type &operator[](size_t index) const; private: Type *memptr_; };
并尝试实例化一个自动变量,如下所示:
AlignedMemory blah(512,512);
这会出现以下错误:
src/cpfs/entry.cpp:438: error: missing template arguments before ‘blah’
我究竟做错了什么? void不是允许的默认参数吗?
解决方法
我想你需要写:
AlignedMemory<> blah(512,512);
见14.3 [temp.arg] / 4:
When default template-arguments are used,a template-argument list can be empty. In that case the empty
<>
brackets shall still be used as the template-argument-list.