我找不到编译时间隔检查器,所以我尝试了一些开发自己的方法,你应该输入有问题的值,最小值和最大值,这样如果有问题的值,检查器将返回true在两个端点之间.
我的第一种方法是能够比较整数,它看起来像这样:
template<int Val, int LoEnd, int HiEnd>
struct is_in_interval : public std::integral_constant<bool, Val >= LoEnd && Val <= HiEnd>::type
{};
Run Code Online (Sandbox Code Playgroud)
对函数的调用看起来像
bool inside = is_in_interval<3, 1, 10>::value;
Run Code Online (Sandbox Code Playgroud)
这似乎有效.如果低端高于高端,我甚至可以在编译时使其失败:
template<int val, int LoEnd, int HiEnd>
struct is_in_interval : public std::integral_constant< typename std::enable_if<LoEnd <= HiEnd, bool>::type, val >= LoEnd && val <= HiEnd>::type
{};
Run Code Online (Sandbox Code Playgroud)
为了能够比较我想出的任何价值:
template<typename T>
struct is_in
{
template<T val, T LoEnd, T HiEnd>
struct closed_interval : public std::integral_constant< typename std::enable_if<LoEnd <= HiEnd, bool>::type, val >= LoEnd && val …Run Code Online (Sandbox Code Playgroud) 我对C++中C风格数组的维度有疑问.在使用声明/ typedef时,使用多个维度时,维度似乎很奇怪.例如:
using A1 = int[23]; //! << A1 = int[23]
using A2 = A1[4]; //! << A2 = int[4][23]
std::cout << std::is_same<int[23][4], A2>::value << std::endl; //false
std::cout << std::is_same<int[4][23], A2>::value << std::endl; //true
Run Code Online (Sandbox Code Playgroud)
我认为A2的类型是int [23] [4]而不是int [4] [23].在下面的代码段中观察到相同的行为:
template<typename T>
struct ArrayTest;
template<typename T, size_t N>
struct ArrayTest<T[N]>
{
using type = T;
};
ArrayTest<int[23][2][45]>::type A3; //! T is int[2][45], N is 23
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我认为类型将是int [23] [2]而不是int [2] [45].有谁知道为什么这样的类型推断?我试图在标准中找到解释,但我想我看起来不够努力.