Re: [分享] typedef dereference of a type

看板C_and_CPP (C/C++)作者 (Acquire higher)時間15年前 (2011/01/01 23:50), 編輯推噓0(005)
留言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
dummy 少了 static, 不過這樣可能出來參照型態
01/01 23:52, 1F

01/02 00:30, , 2F
那可以 decltype(*T()+*T()); ... 搞笑用的 多謝l大
01/02 00:30, 2F

01/02 00:38, , 3F
這樣除了 operator* 這操作, value_type 又必須提供
01/02 00:38, 3F

01/02 00:38, , 4F
operator+ 了 @_@
01/02 00:38, 4F

01/02 00:40, , 5F
恩恩 所以說是搞笑的 XD
01/02 00:40, 5F
文章代碼(AID): #1D7qrNrF (C_and_CPP)
文章代碼(AID): #1D7qrNrF (C_and_CPP)