11

footemplate-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パラメータへの名前は私の知る限り無視されます.

それで、質問は何ですか?

  1. 私の推測は正しいですか?template-template パラメーターの名前付きテンプレート パラメーターの名前は無視されますか?
  2. 私が間違っていて、全体を誤解している場合、名前付きパラメーターを template-template パラメーターに使用することはできますか? 役に立つ例をいくつか挙げていただけますか?

#2の有用な例については、template-template パラメーターの名前付きテンプレート パラメーターを使用してのみ達成できるものを参照しています。

4

1 に答える 1

10

[basic.scope.temp]/p1:

テンプレートtemplate-parameterのテンプレート パラメーターの名前の宣言領域は、 その名前が導入された最小のtemplate-parameter-listです。

(それを10回言ってみてください。)

そのリスト内で使用できます。例えば、

template < template<class T, T t> class TP > class foo {};
//                           ^  ^-----T's scope ends here
//                           |
//                           T can be used here

foo<std::integral_constant> bar;
于 2015-03-11T16:50:27.267 に答える