2011-01-09 16 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はデフォルトパラメータではありませんか?

+0

識別子「buf」を含むコードはどこにもありますか? –

+1

@Charles: 'buf'はタイプミスです。これを見てください:http://www.ideone.com/32gVl ...何かが心の前に欠けています。 :P – Nawaz

答えて

11

私はあなたが書く必要があると思います:

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.

5

あなたの構文が間違っている:

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

正しい構文これはです:

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

エラーメッセージ自体がこのヒントを示します。それをもう一度見てください:

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

PS:確かに 'buf'はタイプミスです。あなたはあなたの変数の名前である 'blah'を書いたかったのです!

関連する問題