模板参数仅作为函数的返回值
生活随笔
收集整理的這篇文章主要介紹了
模板参数仅作为函数的返回值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
程序中寫過這樣幾個函數:
extractUInt
?1unsigned?int?extractUInt(byte?*element)
?2{
?3????byte?id?=?0;
?4????byte?*decodedUInt?=?NULL;
?5????unsigned?short?decodedUIntLength?=?ElementHandler::decode(element,?&id,?&decodedUInt);
?6????unsigned?int?ui?=?0;
?7????memcpy_s(&ui,?sizeof(ui),?decodedUInt,?decodedUIntLength);
?8
?9????delete?[]decodedUInt;
10????return?ui;
11}
extractUShort
?1unsigned?short?extractUShort(byte?*element)
?2{
?3????byte?id?=?0;
?4????byte?*decodedUShort?=?NULL;
?5????unsigned?short?decodedUShortLength?=?ElementHandler::decode(element,?&id,?&decodedUShort);
?6????unsigned?short?us?=?0;
?7????memcpy_s(&us,?sizeof(us),?decodedUShort,?decodedUShortLength);
?8
?9????delete?[]decodedUShort;
10????return?us;
11}
extractByte
?1byte?extractByte(byte?*element)
?2{
?3????byte?id?=?0;
?4????byte?*decodedByte?=?NULL;
?5????unsigned?short?decodedByteLength?=?ElementHandler::decode(element,?&id,?&decodedByte);
?6????byte?by?=?0;
?7????//memcpy_s(&by,?sizeof(by),?decodedByte,?decodedByteLength);
?8????by?=?*decodedByte;
?9
10????delete?[]decodedByte;
11????return?by;
12}
寫的時候并未覺得異樣,昨天再次查看的時候才發現三個函數語義重復,僅返回值不同,于是想到模板函數。想當然地寫成了這樣:
Code
?1template<class?T>
?2T?extractNumber(byte?*element)
?3{
?4????byte?id?=?0;
?5????byte?*decoded?=?NULL;
?6????unsigned?short?decodedLength?=?ElementHandler::decode(element,?&id,?&decoded);
?7
?8????T?t?=?0;
?9????memcpy_s(&t,?sizeof(t),?decoded,?decodedLength);
10
11????delete?[]decoded;
12????return?t;
13}
編譯時未報錯,但在調用時
1unsigned?short?us?=?extractNumber(element);
會出現編譯錯誤,提示無法為“T”推導模板參數。改成如下模樣即可解決問題:
1unsigned?short?us?=?extractNumber<unsigned?short>(element);
extractUInt
?1unsigned?int?extractUInt(byte?*element)
?2{
?3????byte?id?=?0;
?4????byte?*decodedUInt?=?NULL;
?5????unsigned?short?decodedUIntLength?=?ElementHandler::decode(element,?&id,?&decodedUInt);
?6????unsigned?int?ui?=?0;
?7????memcpy_s(&ui,?sizeof(ui),?decodedUInt,?decodedUIntLength);
?8
?9????delete?[]decodedUInt;
10????return?ui;
11}
extractUShort
?1unsigned?short?extractUShort(byte?*element)
?2{
?3????byte?id?=?0;
?4????byte?*decodedUShort?=?NULL;
?5????unsigned?short?decodedUShortLength?=?ElementHandler::decode(element,?&id,?&decodedUShort);
?6????unsigned?short?us?=?0;
?7????memcpy_s(&us,?sizeof(us),?decodedUShort,?decodedUShortLength);
?8
?9????delete?[]decodedUShort;
10????return?us;
11}
extractByte
?1byte?extractByte(byte?*element)
?2{
?3????byte?id?=?0;
?4????byte?*decodedByte?=?NULL;
?5????unsigned?short?decodedByteLength?=?ElementHandler::decode(element,?&id,?&decodedByte);
?6????byte?by?=?0;
?7????//memcpy_s(&by,?sizeof(by),?decodedByte,?decodedByteLength);
?8????by?=?*decodedByte;
?9
10????delete?[]decodedByte;
11????return?by;
12}
寫的時候并未覺得異樣,昨天再次查看的時候才發現三個函數語義重復,僅返回值不同,于是想到模板函數。想當然地寫成了這樣:
Code
?1template<class?T>
?2T?extractNumber(byte?*element)
?3{
?4????byte?id?=?0;
?5????byte?*decoded?=?NULL;
?6????unsigned?short?decodedLength?=?ElementHandler::decode(element,?&id,?&decoded);
?7
?8????T?t?=?0;
?9????memcpy_s(&t,?sizeof(t),?decoded,?decodedLength);
10
11????delete?[]decoded;
12????return?t;
13}
編譯時未報錯,但在調用時
1unsigned?short?us?=?extractNumber(element);
會出現編譯錯誤,提示無法為“T”推導模板參數。改成如下模樣即可解決問題:
1unsigned?short?us?=?extractNumber<unsigned?short>(element);
轉載于:https://www.cnblogs.com/tonyyang132/archive/2009/10/14/1583123.html
總結
以上是生活随笔為你收集整理的模板参数仅作为函数的返回值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 完美解决ALEXA工具条无法显示或显示白
- 下一篇: SQL 触发器 当修改TEST表中的F