[語法] template 用法
看板C_and_CPP (C/C++)作者POSIX (tedium of chores)時間16年前 (2009/09/30 06:47)推噓2(2推 0噓 4→)留言6則, 3人參與討論串1/2 (看更多)
情況特殊 容我描述一下
首先,
猜測用法是當成namespace來用
到時候餵給template當成參數用(達到限制參數的目的)
struct s_limit {
typedef int iii;
typedef float fff;
};
宣告一個dummy class
宣告成template class 的的緣故
是因為要做測試,測試template parameter是否能夠限制
template<typename T1, typename T2>
class dummy {};
測試用的template class,
限制他的template parameter 只能用s_limit 這個structure(當成namespace用)
template <typename TTT = s_limit >
class test_limit
:public dummy<typename TTT::iii, typename TTT::fff >
{
public:
test_limit(){cout<<"constructor"<<endl;}
~test_limit(){cout<<"destructor"<<endl;}
};
所以main 裡面宣告只能這樣用
int main(){
test_limit<s_limit> foo;
return 0;
}
新版的c++ standard 有針對template argument 做更多的保護
但目前似乎以這種方式可以達到"自訂的保護"
對於template 太強大,弄的我頭昏眼花
小弟我經過整理、簡化過後
簡單的把一些code和大家分享
各位有沒有什麼什麼想法?
對於這些用法有沒有更好的方式?
或是有哪些可以更延伸的討論空間?
歡迎大家指教!
--
○ ○ ◢█████◣ ○ ○ ○
○ ██◤◤◤◤▎ ○ ○
○ ◢█◥██⊙–⊙▏ 幹你媽的!沒看過人魚啊!
██████皿█ ▄▄▄▄▄▄▄ 崖上のboyo
○ ◢█∕█◥██︶◤◤ ○ ○ ○
▂▅▄▆▇ by Airsupply
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.117.176.163
推
09/30 08:43, , 1F
09/30 08:43, 1F
→
09/30 08:44, , 2F
09/30 08:44, 2F
→
09/30 08:46, , 3F
09/30 08:46, 3F
→
09/30 09:39, , 4F
09/30 09:39, 4F
推
09/30 10:14, , 5F
09/30 10:14, 5F
→
09/30 12:33, , 6F
09/30 12:33, 6F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章