Chapter 1-02

来源:互联网 发布:python 高级用法 编辑:程序博客网 时间:2024/05/16 09:34

Please indicate the source:http://blog.csdn.net/gaoxiangnumber1

Welcome to my github:https://github.com/gaoxiangnumber1.

1.9.1

configuration_3.cc

Code                               Review

 

/*

Welcome to the GX_STL!

@author GaoXiang

@blog http://blog.csdn.net/gaoxiangnumber1

@github https://github.com/gaoxiangnumber1

@e-mail gaoxiangnumber1@126.com

@function: test __STL_STATIC_TEMPLATE_MEMBER_BUG, defined in <stl_config.h>

*/

 

#include<iostream>

using namespace std;

 

template<class T>

class TestClass

{

public:  // to test conveniently

       static int data_;

};

 

// allocate memory for static data members and assign initial value

template<>

// if we omit "template<>":error: specializing member ‘TestClass<int>::data_’ requires ‘template<>’ syntax

int TestClass<int>::data_ = 1;

template<>

int TestClass<char>::data_ = 2;

 

int main()

{

       cout << TestClass<int>::data_ << endl;  // should be 1

       cout << TestClass<char>::data_ << endl;  // should be 2

 

       TestClass<int> obj_int1, obj_int2;

       TestClass<char> obj_char1, obj_char2;

       cout << obj_int1.data_ << endl;  // should be 1

       cout << obj_int2.data_ << endl;  // should be 1

       cout << obj_char1.data_ << endl;  // should be 2

       cout << obj_char2.data_ << endl;  // should be 2

 

       obj_int1.data_ = 3;

       obj_char2.data_ = 4;

 

       cout << obj_int1.data_ << endl;  // should be 3

       cout << obj_int2.data_ << endl;  // should be 3

       cout << obj_char1.data_ << endl;  // should be 4

       cout << obj_char2.data_ << endl;  // should be 4

 

       return 0;

}

 

/*

The output:

1

2

1

1

2

2

3

3

4

4

*/

Configuration_5.cc

Code                               Review

/*

Welcome to the GX_STL!

@author GaoXiang

@blog http://blog.csdn.net/gaoxiangnumber1

@github https://github.com/gaoxiangnumber1

@e-mail gaoxiangnumber1@126.com

@function: test class template partial specialization

                                    ( <C++ Primer> 876: except the general design for class

                                    template, specialize some design for some template arguments). That is, test

                                    __STL_CLASS_PARTIAL_SPECIALIZATION in <stl_config.h>

*/

 

#include<iostream>

using namespace std;

 

// general design

template<class I, class O>

struct TestClass

{

       TestClass()

       {

              cout << "I, O\n";

       }

};

 

//  class template partial specializations

template<class T>

struct TestClass<T*, T*>

{

       TestClass()

       {

              cout << "T*, T*\n";

       }

};

 

//  class template partial specializations

template<class T>

struct TestClass<const T*, T*>

{

       TestClass()

       {

              cout << "const T*, T*\n";

       }

};

 

int main()

{

       TestClass<int, char> object1;  // output "I, O"

       TestClass<int*, int*> object2;  // output "T*, T*"

       TestClass<const int*, int*> object3;  // output "const T*, T*"

 

       return 0;

}

 

/*

Output:

I, O

T*, T*

const T*, T*

*/

Configuration_8.cc

Code                               Review

/*

Welcome to the GX_STL!

@author GaoXiang

@blog http://blog.csdn.net/gaoxiangnumber1

@github https://github.com/gaoxiangnumber1

@e-mail gaoxiangnumber1@126.com

@function: test __STL_MEMBER_TEMPLATES in <stl_config.h>

*/

 

#include<iostream>

using namespace std;

 

class alloc

{

};

 

template<class T, class Alloc = alloc>

class vector

{

public:

       typedef T value_type;

       typedef value_type* iterator;

 

       template<class I>

       void insert(iterator position, I first, I last)

       {

              cout << "insert()\n";

       }

};

 

int main()

{

       int test[] = {0, 1, 2, 3, 4};

       vector<int> x;

       vector<int>::iterator ite;

       x.insert(ite, test, test + 5);  // output "insert()"

 

       return 0;

}

Configuration_10.cc

code                                review

/*

Welcome to the GX_STL!

@author GaoXiang

@blog http://blog.csdn.net/gaoxiangnumber1

@github https://github.com/gaoxiangnumber1

@e-mail gaoxiangnumber1@126.com

@function: test __STL_LIMITED_DEFAULT_TEMPLATES in <stl_config.h>

*/

 

#include<iostream>

using namespace std;

 

class alloc

{

};

 

template<class T, class Alloc = alloc, size_t BufSiz = 0>

class deque

{

public:

       deque()

       {

              cout << "deque\n";

       }

};

 

// according previous parameter value "T", set next parameter Sequence's

// default value as "deque<T>"

template<class T, class Sequence = deque<T> >

class stack

{

public:

       stack()

       {

              cout << "stack\n";

       }

private:

       Sequence c;

};

 

int main()

{

       stack<int> s;  // output "deque\nstack"

 

       return 0;

}

Configuration_11.cc

Code

Review

/*

Welcome to the GX_STL!

@author GaoXiang

@blog http://blog.csdn.net/gaoxiangnumber1

@github https://github.com/gaoxiangnumber1

@function: test __STL_NON_TYPE_TMPL_PARAM_BUG in <stl_config.h>

*/

 

#include<iostream>

using namespace std;

 

class alloc

{

};

 

inline size_t deque_buf_size(size_t n, size_t sz)

{

       return n != 0 ? n : (sz < 512 ? size_t(512 / sz) : size_t(1));

}

 

template<class T, class Ref, class Ptr, size_t BufSiz>

struct deque_iterator

{

       typedef deque_iterator<T, T&, T*, BufSiz> iterator;

       typedef deque_iterator<T, const T&, const T*, BufSiz> const_iterator;

 

       static size_t buffer_size()  // we can call static member function without object!

       {

              return deque_buf_size(BufSiz, sizeof(T));

       }

};

 

template<class T, class Alloc = alloc, size_t Buf_Siz = 0>

class deque

{

public:

       typedef deque_iterator<T, T&, T*, Buf_Siz> iterator;

};

 

int main()

{

       // we CAN call static member function without object!!!

       cout << deque<int>::iterator::buffer_size() << endl;  // 128

       cout << deque<int, alloc, 64>::iterator::buffer_size() << endl;  // 64

 

       return 0;

}

Welcome to my github:https://github.com/gaoxiangnumber1.

Please indicate the source:http://blog.csdn.net/gaoxiangnumber1.

0 0