2012-01-07 52 views
1

構造函數和複製構造函數如何查找此可變參數模板類?variadic模板類中的(簡單)構造函數

struct A {}; 
struct B {}; 

template < typename Head, 
      typename... Tail> 
struct X : public Head, 
      public Tail... 
{ 
    X(int _i) : i(_i) { } 

    // add copy constructor 

    int i; 
}; 

template < typename Head > 
struct X<Head> { }; 

int main(int argc, const char *argv[]) 
{ 
    X<A, B> x(5); 
    X<A, B> y(x); 

    // This must not be leagal! 
    // X<B, A> z(x); 

    return 0; 
} 
+0

像其他人一樣嗎? – 2012-01-07 14:11:26

回答

1
template < typename Head, 
      typename... Tail> 
struct X : public Head, 
      public Tail... 
{ 
    X(int _i) : i(_i) { } 

    // add copy constructor 
    X(const X& other) : i(other.i) {} 

    int i; 
}; 

模板類中,X作爲一種手段X<Head, Tail...>,所有X用不同的模板參數是不同的類型,所以X<A,B>拷貝構造函數將不匹配X<B,A>

演示:http://ideone.com/V6g35

相關問題