The following code is based on a snippet from here. I want a templated function that accepts a reference to an array of size that is deduced when the function is instantiated:
template<int size>
void myFunction( SomeType(¶m)[size] )
{
//use param and size here
}
//called like this:
SomeType array[SomeConstant];
myFunction( array ); //SomeConstant magically gets into the function as "size"
Now I'm confused with SomeType(¶m)[size]. I'd expect the following to work:
template<int size>
void myFunction( (SomeType[size])& param ) {}
but it wouldn't compile.
Why do I need such weird syntax for "reference to array of fixed size"?
myFunctiongetting called with the wrong size.std::string foo = "foo";uses a fixed-size array. It is hardly horrifying. You're wrongly assuming the copying of data willy-nilly into buffers with ad-hoc bounds checking. That is one possible use of fixed-size arrays, and it's inadvisable to do it in a language where you can avoid it since "ad-hoc" checking frequently turns out to be "bug-ridden" checking...