Why are arrays of references illegal?
Answering to your question about standard I can cite the C++ Standard §8.3.2/4:
There shall be no references to references, no arrays of references, and no pointers to references.
That's because references are not objects and doesn't occupy the memory so doesn't have the address. You can think of them as the aliases to the objects. Declaring an array of nothing has not much sense.
Why it is impossible to create an array of references in c++?
Because indexation into an array is actually defined in terms of an implicit conversion to a pointer, then pointer arithmetic. So to support this, you'd have to also support pointers to references, and define what pointer arithmetic means on them.
C++: why can't we have references to references or array of references?
Pointers are mutable (if non-const), references never. Thus there is no point having a pointer or reference to a reference.
Also, a reference must always refer to something - there is no such thing as a null reference. This is why there can be no arrays of references, as there is no way to default instantiate references inside an array to a meaningful value.
declaration of ‘args’ as array of references Error
The error is pretty much saying everything. std::wstring& args[]
is array ([]
) of wstring (std::wstring
) references (&
). You cannot have array of references - see Why are arrays of references illegal?.
Note: you're coding in C++, main function should be following:
int main(int argc, char *argv[])
{
// Your code
return 0;
}
EDIT:
And AFAIK main function cannot be in any namespace.
Also, there is one more problem with your code - even if we could create array of references, there is not stored information about length of the array. You couldn't use it except first element!
Anyway, you can do following (replaced wstring
with string
because I'm lazy):
#include <vector>
#include <string>
namespace ConsoleApp
{
void Main(std::vector<std::string> &args)
{
}
}
int main(int argc, char *argv[])
{
std::vector<std::string> args;
args.resize(argc);
for(int i = 0; i < argc; ++i)
{
args[i] = argv[i];
}
ConsoleApp::Main(args);
return 0;
}
Technicalities involved with array of references
As the error says, you can't have an array of references. It looks like you're actually trying to pass an array by reference. You can (sort of) do that by changing it to take a pointer to the start of the array:
void repfunc(int a[], int size) // equivalent to "int * a"
Since arrays are convertible to pointers, this will do the right thing if the argument is an array
int a[4] = {1,2,3,4};
repfunc(a, sizeof a / sizeof a[0]);
Alternatively, you could accept a reference to an array of the correct size:
template <size_t size>
repfunc(int (&a)[size])
allowing the size to be deduced automatically
int a[4] = {1,2,3,4};
repfunc(a);
but note that this will only work if a
is actually an array; not, for example an pointer to a dynamically allocated array.
Reference to an array of unknown bound (C++)
The problem I am facing comes from the fact that a pointer to an array of unknown bound is, by my understanding, illegal C++.
You're mistaken. Pointer to an array of unknown bound is not illegal in C++.
I am in fact invoking undefined behaviour. (Or, possibly, some non-standard compiler extension?)
Neither (as long as the pointer is valid). The shown function is standard conforming even if T
is an array of unknown bound.
why are pointers and references to arrays of unknown bound illegal?
They aren't illegal.
There used to be a special case that pointers and references to arrays of unknown bound were illegal as function parameters. That was made legal in a defect resolution in 2014
Related Topics
Proper Stack and Heap Usage in C++
Where Exactly Does C++ Standard Say Dereferencing an Uninitialized Pointer Is Undefined Behavior
Boost Asio Async_Write: How to Not Interleaving Async_Write Calls
Best Way to Extract a Subvector from a Vector
How to Efficiently Perform Double/Int64 Conversions With Sse/Avx
"\N" or '\N' or Std::Endl to Std::Cout
Registering a Cpp Dll into Com After Installation Using Wix Msi Installer
Read Numeric Data from a Text File in C++
Are Exceptions in C++ Really Slow
What Are Some Uses of Decltype(Auto)
Examples of When a Bitwise Swap() Is a Bad Idea
Problem Sorting Using Member Function as Comparator
Why Are Function Pointers and Data Pointers Incompatible in C/C++
What Is the Purpose of Std::Launder
Is There Any Real Risk to Deriving from the C++ Stl Containers