参照の配列は何故作れないか
さて問題です。次のコードの実行結果はどうなるでしょうか?
#include <iostream>
using namespace std;
int main(){
int a = 1, b = 2, c = 3;
int &n[3] = {a, b, c};
for(int i = 0; i < 3; ++i)
cout << n[i] << ' ' ;
cout << endl;
return 0;
} |
見出しを見た皆様のお察しの通り、正解は、「そもそもコンパイルされない」です。
|
code.cpp: In function `int main()': code.cpp:7: error: declaration of `n' as array of references code.cpp:10: error: `n' undeclared (first use this function) code.cpp:10: error: (Each undeclared identifier is reported only once for each function it appears in.) |
このように、C++では参照の配列を作成することは出来ません。
しかし、一体何故でしょうか?
参照の配列によって簡潔に記述できるような処理に出くわすこともままありますし、参照の配列を作ることが原理的に不可能であるとは到底思えません。
それなのに、何故禁止されているのでしょう?
