考慮以下幾種類型C++中是否存在錯誤類型?
template <typename T1, typename T2, typename T3>
struct either_or
{
/* Here I need such an error type that says "Sorry, T1 is not an accepting type." */
typdef error<T1> type;
};
template <typename T1, typename T3>
struct either_or <T1, T1, T3>
{
typedef T1 type; //T1 Ok
};
template <typename T1, typename T2>
struct either_or <T1, T2, T1>
{
typedef T1 type; //T1 Ok
};
/* Here is function that might accept error type variable */
template <typename T>
void foo(typename either_or<T, char, unsigned char>::type x)
{
/*print char or unsigned char except that T is not printable*/
}
是否有C++的類型系統錯誤類型在這種情況下使用?如果沒有,我能否意識到它或如何?
聽起來像你可能想'std :: enable_if' – Flexo 2012-07-09 21:06:32
@Flexo嗯......我已經檢查了它以及std :: conditional和std :: is_same在C++ 11中。但是我真的需要我的函數來明確地告訴它它是什麼:-( – Yang 2012-07-09 21:10:50