PCDVD數位科技討論區
PCDVD數位科技討論區   註冊 常見問題 標記討論區為已讀

回到   PCDVD數位科技討論區 > 其他群組 > 七嘴八舌異言堂
帳戶
密碼
 

回應
 
主題工具
學長壞壞
Basic Member
 
學長壞壞的大頭照
 

加入日期: Jun 2012
您的住址: ?????
文章: 12
C++ 定義特殊化樣版

請問關於C++ 定義特殊化樣版的問題
使用Dev-C++ 5.11
原始碼如下:

#include <iostream>
#include <iomanip>
using namespace std;

template<class T> T larger(T a, T b);
template<> long* larger<long*>(long* a, long* b);

int main()
{
cout << endl;
cout << "Larger of 1.5 and 2.5 is " << larger(1.5, 2.5) << endl;
cout << "Larger of 3.5 and 4.5 is " << larger(3.5, 4.5) << endl;

int value1 = 35;
int value2 = 45;

cout << "Larger of " << value1 << " and " << value2 << " is "
<< larger(value1, value2)
<< endl;

int a = 9;
int b = 8;
cout << "Larger of " << a << " and " << b << " is "
<< larger(a,b)
<< endl;

cout << "Larger of " << a << " and " << b << " is "
<< *larger(&a, &b)
<< endl;

return 0;
}

template<class T> T larger(T a, T b)
{
cout << "standard version " << endl;
return a>b ? a : b;
}

template<> long* larger<long*>(long* a, long* b)
{
cout << "specialization version " << endl;
return *a>*b ? a : b;
}

預期的結果是:
standard version
Larger of 1.5 and 2.5 is 2.5
standard version
Larger of 3.5 and 4.5 is 4.5
standard version
Larger of 35 and 45 is 45
standard version
Larger of 9 and 8 is 9
specializationd version
Larger of 9 and 8 is 9

實際的結果是:
standard version
Larger of 1.5 and 2.5 is 2.5
standard version
Larger of 3.5 and 4.5 is 4.5
standard version
Larger of 35 and 45 is 45
standard version
Larger of 9 and 8 is 9
standard version
Larger of 9 and 8 is 9


請問為何不能使用特殊化樣版?
謝謝!
     
      
舊 2021-07-19, 09:51 PM #1
回應時引用此文章
學長壞壞離線中  
chou124
New Member
 

加入日期: Jun 2008
文章: 3
int a = 9;
int b = 8;
改成
long a = 9;
long b = 8;
試試看
 
舊 2021-07-19, 10:07 PM #2
回應時引用此文章
chou124離線中  
學長壞壞
Basic Member
 
學長壞壞的大頭照
 

加入日期: Jun 2012
您的住址: ?????
文章: 12
引用:
作者chou124
int a = 9;
int b = 8;
改成
long a = 9;
long b = 8;
試試看

非常謝謝你 !!!
舊 2021-07-19, 10:15 PM #3
回應時引用此文章
學長壞壞離線中  


回應


POPIN
主題工具

發表文章規則
不可以發起新主題
不可以回應主題
不可以上傳附加檔案
不可以編輯您的文章

vB 代碼打開
[IMG]代碼打開
HTML代碼關閉



所有的時間均為GMT +8。 現在的時間是09:37 AM.


vBulletin Version 3.0.1
powered_by_vbulletin 2025。