c++ - 为什么不允许使用此默认模板参数?

标签 c++ templates g++ default-parameters

我有以下类(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_;
};

并尝试像这样实例化一个自动变量:

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.

关于c++ - 为什么不允许使用此默认模板参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4638956/

相关文章:

c++ - 找出所有会导致此代码无限循环的 3 个错误

c++ - 如何在 QQmlListProperty 属性中正确通知?

c++ - (cpp) 从常量字符串到 char * 的旧转换

c++ - sfinae 上下文中的模板变量

c++ - 迭代器没有匹配的构造函数

c++ - 线程中使用锁是否会使其他不使用锁的线程减慢速度?

c++ - 如何在 C++ 中创建具有非标准顺序的整数集?

c++ - 为 CUDA 应用程序在自定义类 int2_、float2_ 和 double2_ 之间重载 operator=

g++ - 忽略 LD_LIBRARY_PATH 并坚持使用链接时通过 -rpath 给出的库

compiler-errors - 重定位被截断以适应错误 : R_X86_64_32 against symbol