![](/img/trans.png)
[英]Why using 0 as default non type template parameter for void* is not allowed
[英]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_;
};
並嘗試實例化一個自動變量,如下所示:
AlignedMemory blah(512, 512);
這會出現以下錯誤:
src / cpfs / entry.cpp:438:錯誤:'blah'之前缺少模板參數
我究竟做錯了什么? void
不是允許的默認參數嗎?
我想你需要寫:
AlignedMemory<> blah(512, 512);
見14.3 [temp.arg] / 4:
使用默認模板參數時 , 模板參數列表可以為空。 在這種情況下,空
<>
括號仍應用作模板參數列表 。
你的語法錯了:
AlignedMemory blah(512, 512); //wrong syntax
正確的語法是這樣的:
AlignedMemory<> blah(512, 512); //this uses "void" as default type!
錯誤消息本身給出了這個提示。 再看一遍:
src / cpfs / entry.cpp:438:錯誤:'buf' 之前 缺少模板參數
PS:我確定'buf'是一個錯字。 你想寫'blah' - 變量的名字!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.