发信人: muyang008(operator)
整理人: wenbobo(2003-08-23 10:55:55), 站内信件
|
C++ Primer p516
主题:funtion template explicit specialization + argument deduction
测试结果:VC6 表现太宽松,不严谨。
实例:
#001 #include <iostream>
#002 using namespace std;
#003
#004 template <class T1, class T2, class T3>
#005 T1 sum(T2 op1, T3 op2)
#006 {
#007 cout << "generic form" << endl;
#008 return static_cast<T1>(op1+op2);
#009 }
#010
#011 template<> double sum(float, float);
#012 //上一行在 VC6 竟然可以通过,差劲。
#013 //上一行在 bcb4 出现 e2423: explicit specialization or instantiation
#014 // of non-existing template 'sum'
#015 //上一行在 G++ 出现 : template-id `sum<>' for `sum<>(float, float)'
#016 // does not match any template decaration
#017
#018 // T1 明白指定为 double, T2 推导为 float, T3 推导为 float
#019 template<> double sum<double>(float op1, float op2)
#020 {
#021 cout << "specialization form1" << endl;
#022 return static_cast<double>(op1+op2);
#023 }
#024
#025 // T1, T2, T3 明白指定为 int, char, char
#026 template<> int sum<int, char, char>(char op1, char op2)
#027 {
#028 cout << "specialization form2" << endl;
#029 return static_cast<int>(op1+op2);
#030 }
#031
#032 void main()
#033 {
#034 int i=5;
#035 char c='a';
#036 float f=4.5;
#037 double d=6.5;
#038
#039 cout << sum<int>(i, i) << endl; // generic form 10
#040 cout << sum<double>(f, f) << endl; // specialization form1 9
#041 cout << sum<int>(c, c) << endl; // specialization form2 194
#042 }
■C++ Primer p554
主题:function try block
测试结果:VC6[x] BCB4[x] G++[o]
实例:
#001 #include <iostream>
#002 using namespace std;
#003
#004 class popOnEmpty { /* ... */ };
#005 class pushOnFull { /* ... */ };
#006
#007 int main()
#008 try {
#009 throw popOnEmpty();
#010 throw pushOnFull();
#011 return 0;
#012 }
#013 catch ( pushOnFull ) {
#014 cout << "catch pushOnFull" << endl;
#015 }
#016 catch ( popOnEmpty ) {
#017 cout << "catch popOnEmpty" << endl; // <-- 执行结果:此行。
#018 }
■C++ Primer p564
主题:exception specification
测试结果:BCB4 表现佳,G++ 尚可,VC6 粗糙
实例:
#001 #include <iostream>
#002 using namespace std;
#003
#004 class popOnEmpty { /* ... */ };
#005 class pushOnFull { /* ... */ };
#006
#007 void func1() throw (pushOnFull);
#008
#009 void func1() throw (pushOnFull)
#010 {
#011 throw popOnEmpty(); // BCB4 warning: Throw expression violates
#012 // exception specification in function
#013 // func1() throw(pushOnFull)
#014 // VC6 : no error, no warning
#015 // G++ : no error, no warning
#016
#017 throw pushOnFull(); // BCB4 Warning : Unreachable code in function
#018 // func1() throw(pushOnFull)
#019 // VC6 : no error, no warning
#020 // G++ : no error, no warning
#021 }
#022
#023 int main()
#024 {
#025 try {
#026 func1();
#027 return 0;
#028 }
#029 catch ( pushOnFull ) {
#030 cout << "catch pushOnFull" << endl;
#031 }
#032 catch ( popOnEmpty ) {
#033 cout << "catch popOnEmpty" << endl;
#034 }
#035 }
#036 // 执行结果:
#037 // BCB4: Abnormal program termination
#038 // G++ : none(我想是唤起了 C++ standard library function unexpected(),
#039 // 後者唤起 terminate(),其内唤起 abort()。
#040 // VC6 : catch popOnEmpty(我认为 VC6 对於 exception spec. 的处理太粗糙)
■C++ Primer p643 中
主题:直接在 class 内针对 static const integral data member 给予初值
(所谓 in-class initialization)
测试结果:VC6[x] BCB4[o] G++[o]
实例:
#001 #include <iostream.h>
#002
#003 class Account {
#004 public:
#005 static const int namesize = 16; // <== in-class initialization
#006 };
#007
#008 const int Account::namesize;
#009
#010 void main()
#011 {
#012 cout << Account::namesize << endl;
#013 }
■C++ Primer p834
主题:class templates 内的 friend function
测试结果:VC6[x] BCB4[x] G++[o]
叁考:请见稍後 ■C++ Primer p1090 对於 "VC6 的 friend functions" 的深入说明。
实例:
#001 #include <iostream>
#002 using namespace std;
#003
#004 // 以下的 forward declaration 非常重要,见 p834 L-8
#005 template <typename T> class A;
#006 template <typename T> ostream& operator<< (ostream& os, const A<T>& a);
#007
#008 template <typename T> class B;
#009 template <typename T> ostream& operator<< (ostream& os, const B<T>& b);
#010
#011 // 以下以 class A 和 class B 模拟 class Queue 和 class QueueItem 之间的关系
#012
#013 template <typename T>
#014 class A
#015 {
#016 // 以下的 <T> 非常重要,见 p834 L-6
#017 friend ostream& operator<< <T>(ostream& os, const A<T>& a);
#018 public:
#019 A (T i) : _i(i) {
#020 front = new B<T>(i);
#021 back = new B<T>(i);
#022 }
#023 // 为求完整,应再设计 dtor 以避免 memory leak.
#024
#025 private:
#026 T _i;
#027 B<T>* front;
#028 B<T>* back;
#029 };
#030
#031 template <typename T>
#032 ostream& operator<< (ostream& os, const A<T>& a)
#033 {
#034 os << '<' ;
#035 os << *(a.front) << ' ';
#036 os << *(a.back) << ' ' ;
#037 os << '>' << endl;
#038 return os;
#039 }
#040
#041 template <typename T>
#042 class B
#043 {
#044 friend ostream& operator<< <T>(ostream& os, const B<T>& b);
#045 public:
#046 B (T i) : _item(i) { }
#047 private:
#048 T _item;
#049 };
#050
#051 template <typename T>
#052 ostream& operator<< (ostream& os, const B<T>& b)
#053 {
#054 os << b._item; // BCB4 error: _item is not accessible. why?
#055 return os;
#056 }
#057
#058 void main()
#059 {
#060 A<int> a1(5);
#061 A<float> a2(5.4);
#062 A<char> a3('a');
#063
#064 cout << a1 << a2 << a3 << endl;
#065 /* output :
#066 <5 5 >
#067 <5.4 5.4 >
#068 <a a >
#069 */
#070 }
|
|