0

クラステンプレートの特殊例の暗黙のインスタンスを理解する:ので、標準の<a href="http://www.eel.is/c++draft/temp.inst#2" rel="nofollow noreferrer">this section</a>が(私の質問はインラインある)この例を示します

template<class T, class U> 
struct Outer { 
    template<class X, class Y> struct Inner;  // Where are X and Y from? Is this defining a struct? 
    template<class Y> struct Inner<T, Y>;   // Is this defining a struct specialization? If so what is Y? 
    template<class Y> struct Inner<T, Y> { };  // I feel like this is a redefinition of the line above, could it ever not be? 
    template<class Y> struct Inner<U, Y> { }; 
}; 

確かに私は私ができる標準的な原因のリンク部分を理解することはできませんここで起こっていることを理解する。私はちょうど周りに飛んでtemplateのすべてが混乱していると思うが、誰かが行ごとに行けば何が起こっているか教えてくれれば非常に役に立つだろう。

+0

@Downvoter。 C++の質問に必須のdownvoteを提供していただけでしたか、実際にこの質問に間違っていましたか? –

答えて

3
template<class X, class Y> struct Inner;  
// Where are X and Y from? Is this defining a struct? 

これは、テンプレート構造体InnerXの宣言であり、Yは、テンプレートパラメータです。

template<class Y> struct Inner<T, Y>;   
// Is this defining a struct specialization? If so what is Y? 

これはYは、テンプレートパラメータである、部分特殊の宣言です。

template<class Y> struct Inner<T, Y> { };  
// I feel like this is a redefinition of the line above, could it ever not be? 

これは部分特殊の定義ではありませんので、ここには再定義。

は次にとしてそれらを使用します。ここでは

Outer<foo1, foo2>::Inner<foo3, foo4>* i1; 
// the primary template is used, with T=foo1, U=foo2, X=foo3, Y=foo4 
// btw the primary template is not defined in this example 

Outer<foo1, foo2>::Inner<foo1, foo3> i2; 
// the 1st partial specialization is used, with T=foo1, U=foo2, Y=foo3 

Outer<foo1, foo2>::Inner<foo2, foo3> i3; 
// the 2st partial specialization is used, with T=foo1, U=foo2, Y=foo3 
+0

私たちは実際に行うことができます: '外部 ::内部 i1;'?それは定義されていますか? –

+3

@JonathanMeeいいえ。未定義のエラーが発生します。 – songyuanyao

1

あなたが行く:私はこれはC++をタグ付けして知っている

template<class T, class U> 
struct Outer { 
    // Forward-declares a struct with two template parameters 
    template<class X, class Y> struct Inner; 

    // Forward-declares a partial specialization of Inner 
    // (Y is its only template parameter) 
    template<class Y> struct Inner<T, Y>; 

    // Defines the forward-declared partial specialization 
    template<class Y> struct Inner<T, Y> { }; 

    // Declares and defines another partial specialization 
    // (Y is its only template parameter) 
    template<class Y> struct Inner<U, Y> { }; 
}; 
関連する問題