クラステンプレートを使用するときの少しの戸惑い

5042 ワード

 1 #include<string>

 2 #include<iostream>

 3 

 4 using namespace std;

 5 

 6 ///        

 7 template<typename T,typename N>

 8 class my_tuple

 9 {

10 public:

11     T value;

12     N next;

13     my_tuple(T const &v,N const &n):value(v),next(n){}

14 };

15 template<typename T,typename N>

16 my_tuple<T,N> push(T const &v,N const &n)

17 {

18     return my_tuple<T,N>(v,n);

19 }

20 int main()

21 {

22     typedef my_tuple<int ,char> tuple2;

23     typedef my_tuple<float,tuple2> tuple3;

24     typedef my_tuple<std::string,tuple3> tuple4;

25 

26     tuple4 tup4 = push(std::string("awesome"),
27               push(.5f,
push(2,'a'))); 28 cout<<tup4.value<<"," 29 <<tup4.next.value<","; 30 cout<<tup4.next.next.value<<"," 31 <<tup4.next.next.next<<endl; 32 // : awesome,0.52,a 33 // 34 // cout<<tup4.value<<","<<tup4.next.value<","<<tup4.next.next.value<<","<<tup4.next.next.next<<endl; 35 // :test.cpp||error: invalid operands of types 'const char [2]' and 'int' to binary 'operator<<' 36 }

大神たちが見てヒントを与えてくれることを願っています