Why is this default template parameter not allowed? [duplicate]
Asked Answered
T

2

6

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?

Tonsillectomy answered 9/1, 2011 at 11:48 Comment(2)
Do you have any code that includes the identifier buf anywhere?Townsfolk
@Charles : buf is a typo. See this : ideone.com/32gVl ... something is missing before mind. :PCella
T
10

I think that you need to write:

AlignedMemory<> blah(512, 512);

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.

Townsfolk answered 9/1, 2011 at 11:54 Comment(0)
C
5

Your syntax is wrong:

AlignedMemory blah(512, 512); //wrong syntax

Correct syntax is this:

AlignedMemory<> blah(512, 512); //this uses "void" as default type!

The error message itself gives this hint. Look at it again:

src/cpfs/entry.cpp:438: error: missing template arguments before ‘buf’

PS: I'm sure 'buf' is a typo. You wanted to write 'blah' - the name of your variable!

Cella answered 9/1, 2011 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.