2016-04-18 87 views
20

我很困惑:在升級到GCC 6(RC1)後,一些使用std::common_type失敗的模板代碼。我嘗試了叮噹,那也失敗了...所以我一定是做錯了!std :: common_type與type_info的引用

該代碼相當於:

#include <type_traits> 
#include <typeinfo> 

using namespace std; 

// common_type of two const type_info& is ok (compiles ok) 
common_type<const type_info&, const type_info&>::type func1(); 

// common_type of three type_info& is bad...(fails to compile) 
common_type<const type_info&, const type_info&, const type_info&>::type func2(); 

// common_type of two const int& is ok 
common_type<const int&, const int&>::type func3(); 

// common_type of three const int& is ok too! 
common_type<const int&, const int&, const int&>::type func4(); 

第二common_type與三個參數類型的std::type_info const &無法編譯。鏗鏘暗示暗示我使用了雙參數std::common_type,但這是在模板擴展中,我無法控制輸入!

這似乎很奇怪:爲什麼const type_info&與3失敗,但沒有任何其他看似等效類型失敗?

在這裏看到:https://godbolt.org/g/Ob4y0x

+3

'common_type'現在衰變,並導致「有趣」的事情。 –

+0

一些twitter引用指出我這樣一個事實:'std :: common_type'返回Ts的'std :: decay' ......這對我的用例來說是非常煩人的,但是解釋了上面的錯誤。 –

+0

@MattG:你可以隨時用'common_type_t ...>替換'common_type_t '',所以它不應該超級煩人。 –

回答

15

首先,common_type_t<T1, T2>爲(大約)std::decay_t<decltype(true? std::declval<T1>() : std::declval<T2>())>。它會消除類型帶去參照,刪除頂級cv限定,並執行數組到指針和函數到指針的轉換。因此,common_type<const type_info&, const type_info&>::typetype_info。儘管func1的聲明似乎有效,但編寫其定義時會遇到嚴重問題。

common_type_t<T1, T2, T3>common_type_t<common_type_t<T1, T2>, T3>,所以common_type<const type_info&, const type_info&, const type_info&>::typecommon_type<type_info, const type_info&>::type

導致混合值類三元表達,它通過在[expr.cond]的規則將努力使臨時type_info出所選擇的操作數 - 不工作,因爲type_info的拷貝構造函數被刪除。

在SFINAE友好的實現中,導致common_type<const type_info&, const type_info&, const type_info&>沒有成員type。如果你使用非SFINAE友好的實現,你會得到一個硬錯誤。

+0

謝謝,這絕對回答了這個問題。任何想法爲什麼這改變了C++ 14雖然?似乎我錯過了一些東西,如果我的代碼之前工作,但不是現在:) –

+5

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2141 –

+2

@MattG^that(簡而言之,這樣'common_type_t '不是'int &&')。 –

相關問題