为什么不允许使用这个默认模板参数?
我有以下类:
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:错误:“blah”之前缺少模板参数
我做错了什么? void
不是允许的默认参数吗?
I have the following class:
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);
This gives the following error:
src/cpfs/entry.cpp:438: error: missing template arguments before ‘blah’
What am I doing wrong? Is void
not an allowed default parameter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你需要写:
参见14.3 [temp.arg] / 4:
I think that you need to write:
See 14.3 [temp.arg] / 4:
您的语法错误:
正确的语法是这样的:
错误消息本身给出了此提示。再看一遍:
PS:我确信'buf'是一个错字。您想写“blah”——变量的名称!
Your syntax is wrong:
Correct syntax is this:
The error message itself gives this hint. Look at it again:
PS: I'm sure 'buf' is a typo. You wanted to write 'blah' - the name of your variable!