Re: [分享] typedef dereference of a type
看板C_and_CPP (C/C++)作者adxis (Acquire higher)時間15年前 (2011/01/01 23:50)推噓0(0推 0噓 5→)留言5則, 2人參與討論串4/5 (看更多)
※ 引述《yoco315 (眠月)》之銘言:
: 而且感覺很智障,想了一下,可以把這個東西抽離。
: 變成這樣
: template < typename T >
: struct dereference {
: T v ;
: typedef decltype(*v) type ;
: } ;
: template < typename IterType >
: class Foo {
: IterType i ;
: typedef dereference<IterType>::type ValueType ;
: } ;
: 這樣就 Foo 看起來就聰明多了,而我反正根本不會去具現化 dereference,
: 所以也不會有什麼 T 真的生出來,但是這個寫法還是有問題,
: 因為 T 可能沒有 public default constructor。編譯便不會過。
: 又想了一下,最後改成
: template < typename T >
: struct dereference {
: T dummy() ;
: typedef decltype(*dummy()) type ;
不是很了解這一行的意思,用gcc 4.5 測試也編不過
還是 y大 的意思是像下面這樣?
template< typename IterType>
struct deref {
typedef decltype(*IterType()) ValueType;
};
這樣在 default ctor 為 private 的狀況下仍然可以運作
: } ;
: template < typename IterType >
: class Foo {
: IterType i ;
: typedef dereference<IterType>::type ValueType ;
: } ;
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.123.101.180
→
01/01 23:52, , 1F
01/01 23:52, 1F
→
01/02 00:30, , 2F
01/02 00:30, 2F
→
01/02 00:38, , 3F
01/02 00:38, 3F
→
01/02 00:38, , 4F
01/02 00:38, 4F
→
01/02 00:40, , 5F
01/02 00:40, 5F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 4 之 5 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章
11
38