template?
下のような書き方で, 途中に出てくる template って何でしょうか?
template<T,S> struct hoge { typedef typename foo<S>::bar Bar; typedef typename Bar::template xxx<T,S>::yyy zzz; };
正解は, 以下のような文脈でテンプレートクラス(構造体)を参照するためのキーワード.
template<typename S> struct foo { struct bar { template<typename X,typename Y> struct xxx { struct yyy { }; }; }; }; template<typename T,typename S> struct hoge { typedef typename foo<S>::bar Bar; typedef typename Bar::template xxx<T,S>::yyy zzz; }; int main(){ hoge<int,int> x; }
template をつけずに
typedef typename Bar::xxx<T,S>::yyy zzz;
と書いて良さそうに思うが, エラーが出る.
error: non-template ‘xxx’ used as template note: use ‘typename foo<S>::bar::template xxx’ to indicate that it is a template
typename みたいなものか. コンパイラが struct hoge の宣言(あれ?定義?)を読んでいる段階では, まだ foo<S>::bar というクラスは実体化されていないので, xxx という名前がいったい何を指しているのか (型?変数?関数?) 不明であり, template かどうかも不明なので, template と書いてヒントを与えているわけか. なっとく.