foo
template-template パラメーターを使用してテンプレート関数を定義する必要がある場合、通常は次のようにします。
// Notice that the template parameter of class T is unnamed.
template <template <typename> class T> void f() { std::cout << "Yay!\n"; }
template-template パラメーターのテンプレート パラメーターには名前が付いていませんが、このパラメーターに名前を割り当てることができます。
// Now the template parameter of class T is named INNER.
template <template <typename INNER> class T> void f(const INNER &inner)
{ std::cout << inner << " Yay!\n"; }
関数でパラメーターを参照できないため、これはまったく役に立たないINNER
ようです。上記のコードは次のエラーを生成します。
エラー: 'INNER' は型を指定していません
型に名前を付けるためにキーワードがそこにあるのに、 がtypename INNER
型に名前を付けていないのは驚きです。typename
とにかく、これは簡単に修正できます。
// Now INNER is the name of the template parameter of class T and also
// the name of the second template parameter of foo.
template <template <typename INNER> class T, typename INNER> void f(const INNER &inner)
{ std::cout << inner << " Yay!\n"; }
// ...
f<std::valarray, int>(666); // Prints "666 Yay!"
しかし、結局のところ、INNER
パラメーターに名前は必要ありません。
// Now the template parameter of class T is unnamed one more time,
// INNER is the name of the second template parameter of foo.
template <template <typename> class T, typename INNER> void f(const INNER &inner)
{ std::cout << inner << " Yay!\n"; }
// ...
f<std::valarray, int>(666); // Prints "666 Yay!"
そして (私の前に既にお気付きだと思いますが) template-template パラメーターのパラメーターの名前は無視されます! そうでない場合は、 の 2 番目のテンプレート パラメーターと名前が競合する必要があるため、無視されたことは確かfoo
です。
template-template パラメーターのパラメーターの名前が無視される別の例:
// Now T is the name of the template parameter of class T and also
// the name of the template parameter of foo!
template <template <typename T> class T> void f()
{ std::cout << "Yay!\n"; }
// ...
f<std::valarray>(); // prints "Yay!"
指定された型T
は、template-template パラメーターと template-template 自体によって同時に使用されていますか? 私はそうは思いません.template-templateパラメータへの名前は私の知る限り無視されます.
それで、質問は何ですか?
- 私の推測は正しいですか?template-template パラメーターの名前付きテンプレート パラメーターの名前は無視されますか?
- 私が間違っていて、全体を誤解している場合、名前付きパラメーターを template-template パラメーターに使用することはできますか? 役に立つ例をいくつか挙げていただけますか?
#2の有用な例については、template-template パラメーターの名前付きテンプレート パラメーターを使用してのみ達成できるものを参照しています。