#001 int main()
#002 {
#003 int &i = 3; // (1) should be error
#004 // rvalue assign to non-const reference
#005 const int &i2 = 3; // (2) should be ok
#006 // rvalue assign to const reference
#007 int &i3 = int(3); // (3) should be error
#008 // rvalue (temp obj) assign to non-const reference
#009 const int &i4 = int(3); // (4) should be ok
#010 // rvalue (temp obj) assign to const reference
#011 }
#012
#013 // G++ : (1),(3) warning: initialization of non-const reference `int &'
#014 // from rvalue `int'
#015 // jjhou 使用 G++ 2.91.57。
#016 // 據 jyhuang 說,G++ 2.92.2 並不允許通過 (1),(3)。
#017 //
#018 // VC6 : (1),(3) error: 'initializing' : cannot convert from 'const int'
#019 // to 'int &'. A reference that is not to 'const'
#020 // cannot be bound to a non-lvalue
#021 //
#022 // BCB4: (1),(2),(3),(4) warning: Temporary used to initialize 'i'
#023 // in function main ()
例二:
#001 void func1(int i) { }; // pass by value
#002 void func2(int& i) { }; // pass by reference
#003 void func3(int* i) { }; // pass by pointer
#004 void func4(const int& i) { }; // pass by reference
#005
#006 void main()
#007 {
#008 int i;
#009 const int ci = 5;
#010
#011 func1(i); // lvalue-to-rvalue, always ok.
#012 func1(ci);
#013 func2(i);
#014 func2(ci); // (15)
#015 func3(&i);
#016 func3(&ci); // (17)
#017 func4(i);
#018 func4(ci);
#019
#020 func2(int(6)); // (21), rvalue-to-nonconst-reference.
#021 func4(int(6)); // rvalue-to-const-reference, always ok.
#022 }
#023
#024 /*
#025 VC6 :
#026 (15) : error C2664: 'func2' : cannot convert parameter 1 from
#027 'const int' to 'int &'. Conversion loses qualifiers
#028 (17) : error C2664: 'func3' : cannot convert parameter 1 from
#029 'const int *' to 'int *'. Conversion loses qualifiers
#030 (21) : error C2664: 'func2' : cannot convert parameter 1 from
#031 'int' to 'int &'.
#032 A reference that is not to 'const' cannot be bound to a non-lvalue
#033
#034 BCB4 :
#035 Warning (15): Temporary used for parameter 'i' in call to 'func2(int &)'
#036 Error (17): Cannot convert 'const int *' to 'int *'
#037 Error (17): Type mismatch in parameter 'i' in call to 'func3(int*)'
#038 Warning (21): Temporary used for parameter 'i' in call to 'func2(int &)'
#039
#040 G++ :
#041 (15): warning: conversion from `const int' to `int &' discards const
#042 (3) : warning: in passing argument 1 of `func2(int &)'
#043 (17): warning: passing `const int *' as argument 1 of `func3(int *)' discards const
#044 (21): warning: initialization of non-const reference `int &' from rvalue `int'
#045 (3) : warning: in passing argument 1 of `func2(int &)'
#046 */
double(*fp)(rad) = &min; // instantiate 'min', using specified type.
func(fp);
■C++ Primer p503
主題:如果 template function 的函式參數型別是一個 class template,
而實際引數是一個 class,此 class 有個 base class,係從「被指定
做為函式參數」之 class template 身上具現出來,那麼 template 的
引數推導可以順利進行。
測試結果:VC6[x] BCB4[x] G++[x]
實例:
#001 template <class T>
#002 class Array { };
#003
#004 template <class T>
#005 class ArrayRC : public Array<T> { };
#006
#007 template <class T>
#008 T min4(Array<T>& array) { return T(0); }
#009
#010 void main()
#011 {
#012 ArrayRC<int> ia_rc();
#013
#014 // min4() 的函式引數型別是 ArraryRC<int>,其 base class 為 Array<int>,
#015 // 正是 function template min4() 的函式參數型別 Array<T> 的
#016 // 一個具現體(instantiation),所以 min4() 應該可以接受它(書上說可以)
#017 min4(ia_rc); // error in VC6, BCB4, G++2.51.97
#018 }