[英]Need help decoding this typedef
我正在嘗試創建對數組的引用。
它以這種方式工作:
typedef int array_type[100];
int main() {
int a[100];
array_type &e = a; // This works
}
但后來我試圖刪除typedef
,並得到相同的工作。 沒有成功。
int main() {
int a[100];
// int[100] &e = a; // (1) -> error: brackets are not allowed here; to declare an array, place the brackets after the name
// int &e[100] = a; // (2) -> error: 'e' declared as array of references of type 'int &'
}
我對typedef
解釋有什么問題? 我怎么能刪除typedef
,仍然可以獲得相同的功能。
如果你想避免這種混淆,它實際上是一個轉移模板類型的好機會,因為有std::array
。 除此之外,它們提供了一些手段來統一您需要使用的語法,並且在此示例中,消除了引用/數組/的混淆...
int main()
{
std::array<int, 100> a;
std::array<int, 100>& e = a;
}
沒有什么可以阻止你仍然提供類型別名:
using array_type = std::array<int, 100>;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.