既然我们知道概念不是C++ 0x的一部分,我正在寻找方法来限制模板函数中的类型。强制模板函数类型的约束,没有C++ 0x概念
这里有两个例子:
如果我们想确保一个给定类型为整数时,我们可以使用:
template <class N> inline int f(const N n)
{
if ((N)0.1 != 0) // if type of N is floating-point
err()
....
}
如果我们想要确保一个给定的类型是无符号整数,我们可以使用:
template <class N> inline int f(const N n)
{
if ((N)-1 < (N)1) // if type of N is floating-point/signed-integer
err()
....
}
我要寻找创造性的方式ŧ检查额外的限制,这会在运行时导致失败,或者在编译时(没有概念和没有RTTI)导致失败。
有什么建议吗?