为什么不允许此默认模板参数? [英] Why is this default template parameter not allowed?
本文介绍了为什么不允许此默认模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下类:
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_;
};
并试图像这样实例化一个自动变量:
And attempt to instantiate an automatic variable like this:
AlignedMemory blah(512, 512);
这会产生以下错误:
src / cpfs / entry.cpp:438:错误:在'blah'之前缺少模板参数
src/cpfs/entry.cpp:438: error: missing template arguments before ‘blah’
我做错了吗? void
不是允许的默认参数?
What am I doing wrong? Is void
not an allowed default parameter?
推荐答案
需要写:
AlignedMemory<> blah(512, 512);
请参阅14.3 [temp.arg] / 4:
See 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.
这篇关于为什么不允许此默认模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文