2011-01-09 3 views
6

나는 다음과 같은 클래스가 :이 기본 템플릿 매개 변수가 허용되지 않는 이유는 무엇입니까?

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

을 그리고이 같은 자동 변수의 인스턴스를 시도 :

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

내가 잘못 뭐하는 거지 :

AlignedMemory blah(512, 512); 

이 다음과 같은 오류를 제공을 ? void이 허용 된 기본 매개 변수가 아닙니까?

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.

+0

식별자 'buf'가 포함 된 코드가 있습니까? –

+1

@Charles :'buf'는 오타입니다. 이것을보십시오 : http://www.ideone.com/32gVl ... 마음이 있기 전에 뭔가가 빠져 있습니다. :피 – Nawaz

답변

11

은 당신이 쓸 필요가 있다고 생각 이것을 :

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

오류 메시지 자체가이 힌트를 제공합니다. 다시보기 :

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

추 신 : 'buf'는 오타입니다. 변수의 이름 인 'blah'를 쓰고 싶었습니다!

5

귀하의 구문 잘못 :

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

올바른 구문

AlignedMemory<> blah(512, 512); 

14.3 [temp.arg]/4 참조 :

관련 문제