Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 数组的typedef数组的typedef_C++_C_Arrays_Multidimensional Array_Typedef - Fatal编程技术网

C++ 数组的typedef数组的typedef

C++ 数组的typedef数组的typedef,c++,c,arrays,multidimensional-array,typedef,C++,C,Arrays,Multidimensional Array,Typedef,由于标题可能看起来很混乱,让我举个例子: typedef bool foo[2]; typedef foo bar[4]; bar what_am_i; 那么,我是什么我认为是一个[4][2]维度数组,还是一个[2][4]维度数组?它是bool[4][2] 您可以通过静态断言进行验证: static_assert(std::is_same<decltype(what_am_i), bool[4][2]>::value, ""); static_assert(std::is_same

由于标题可能看起来很混乱,让我举个例子:

typedef bool foo[2];
typedef foo bar[4];
bar what_am_i;

那么,
我是什么
我认为是一个
[4][2]
维度数组,还是一个
[2][4]
维度数组?

它是
bool[4][2]
您可以通过静态断言进行验证:

static_assert(std::is_same<decltype(what_am_i), bool[4][2]>::value, "");
static_assert(std::is_same<decltype(what_am_i), bool[2][4]>::value, ""); // failed
static_断言(std::is_same::value,”);
静态断言(std::is_same::value,“”;//失败

<代码> >通过调试程序检查变量后,发现我是正确的——<代码> WHYAMAMI I/CODE >是<代码> [4 ] [2 ] <代码>维数组。< /P> < P>,以完成Slardar Zhang的C++回答C:


它是
bool[4][2]

您可以通过以下任一方法进行验证:

  • sizeof(what__i)/sizeof(*what__i)==4
  • sizeof(*what__i)/sizeof(**what__i)==2

foo
是一个数组,包含
2个
元素,类型为
bool
,即
bool[2]

bar
是一个数组,包含
4
类型为
foo
的元素,即
foo[4]
,每个元素都是一个
bool[2]


然后
什么是
bool[4][2]
当你不知道变量的类型时,一个简单的方法是以下技巧:

 template<class T> struct tag_type{using type=T;};
 template<class T> constexpr tag_type<T> tag{};
有时您会希望通过
decltype(some\u表达式)
生成
some\u类型
,或者您可以
cout
 tag_type<void> t = tag<some_type>;
tag_type<your_guess> t = tag<some_type>;