6

次のコードをコンパイルしようとしていますが、解決できない問題があるようです。

template <int x>
struct count_x
{
   enum { x_size = x };
};

template <typename y>
struct crtp_base
{
   typedef typename y::count_t count_t;
   crtp_base(const count_t&){}
};

template <int x>
struct derived : public crtp_base<derived<x> >
{
   typedef typename count_x<x> count_t;
   typedef crtp_base<derived<x> > base_t;
   derived(const count_t& c) : base_t(c){}
};


int main()
{
   derived<2> d((count_x<2>()));
   return 0;
}

clang 3.1でコンパイルすると、次のエラーが発生します。

c:\clangllvm\code\example.cc:18:21: error: expected a qualified name after 'typename'
   typedef typename count_x<x> count_t;
                    ^
c:\clangllvm\code\example.cc:18:21: error: typedef name must be an identifier
   typedef typename count_x<x> count_t;
                    ^~~~~~~~~~
c:\clangllvm\code\example.cc:18:28: error: expected ';' at end of declaration list
   typedef typename count_x<x> count_t;
                           ^
                           ;
c:\clangllvm\code\example.cc:20:18: error: no template named 'count_t'; did you mean 'count_x'?
   derived(const count_t& c)
                 ^~~~~~~
                 count_x
c:\clangllvm\code\example.cc:2:8: note: 'count_x' declared here
struct count_x
       ^
c:\clangllvm\code\example.cc:20:18: error: use of class template count_x requires template arguments
   derived(const count_t& c)
                 ^
c:\clangllvm\code\example.cc:2:8: note: template is declared here
struct count_x
       ^
5 errors generated.

テンプレートがコンパイル時に決定される方法と、テンプレートが適切なタイミングで型であると決定されるかどうかに関係があると思います。また、「usingbase_t::count_t;」を無駄に追加してみました。それ以外は、コンパイラーによって生成された診断は私を本当に失ってしまいました。このエラーについて読むべき何かについての答えや提案をいただければ幸いです。

4

1 に答える 1

2

count_x<x>は修飾名ではないため(まったく名前がありません::!)、前に。を付けることはできませんtypename

これを修正しても、CRTPベースをインスタンス化するときに、派生型のネストされたtypedefがコンパイラによってまだ認識されていないため、コードは失敗します。この他の質問は、いくつかの選択肢を示しています。

于 2012-10-12T03:11:13.183 に答える