2011-01-09 27 views
6

tengo la clase siguiente:¿Por qué no se permite este parámetro de plantilla predeterminado?

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_; 
}; 

y el intento de crear una instancia de una variable automática como esto:

AlignedMemory blah(512, 512); 

Esto da el siguiente error:

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

¿Qué estoy haciendo mal ? ¿void no es un parámetro predeterminado permitido?

+0

¿Tiene algún código que incluya el identificador 'buf' en cualquier lugar? –

+1

@Charles: 'buf' es un error tipográfico. Mira esto: http://www.ideone.com/32gVl ... algo falta antes de la mente. :PAG – Nawaz

Respuesta

11

creo que tiene que escribir:

AlignedMemory<> blah(512, 512); 

Ver 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.

5

Su sintaxis es incorrecta:

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

sintaxis correcta es esto:

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

El mensaje de error en sí ofrece esta sugerencia. Mira de nuevo:

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

PS: Estoy seguro 'buf' es un error tipográfico. Querías escribir 'blah' - ¡el nombre de tu variable!

Cuestiones relacionadas