【发布时间】:2012-07-09 21:05:23
【问题描述】:
考虑以下类型
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 嗯...我已经检查过它以及 C++11 中的 std::conditional 和 std::is_same 。但我真的需要我的函数明确告诉它卡住了:-(