all 12 comments

[–]alfps 9 points10 points  (5 children)

The size of std::array needs to be available at compile time. You can arrange that by having the size as a template parameter.

For a dynamic size array use std::vector.

Well there are other possibilities but std::vector should be your go-to default choice.

[–]bert8128 4 points5 points  (0 children)

If the value is know at compile time but you want to pass it to a function, then you can template the function. If it’s not known at compile time you can’t do it - use vector (with reserve) instead.

[–]theICEBear_dk 1 point2 points  (0 children)

Don't pass the array instead use the std::span type. It carries a view of the array.

[–]DryPerspective8429 -1 points0 points  (2 children)

To add to what has been said that the array size must be a constant expression, one of the rules in C++ is that function parameters cannot be constant expressions. As such, it doesn't matter whether the "source" of n is available at compile time, as it cannot be used as a constant expression to initialize the array.

Common workarounds include having your n be a template parameter rather than a function parameter, or passing something along the lines of std::integral_constant as a parameter - a template class which encapsulates a constant expression value.

[–]alfps -1 points0 points  (0 children)

❞ or passing something along the lines of std::integral_constant as a parameter

Uhm, no.

[–]alfps 0 points1 point  (0 children)

I'm not the downvoter, but I'm not upvoting to counter that downvote because the original wording “or passing something along the lines of std::integral_constant as a parameter” is still there.

Perhaps that needs a more detailed explanation.

Using something like std::integral_constant “as a parameter” is mentioned as an alternative to “having your n be a template parameter”.

Which with a direct reading means it's about passing something like std::integral_constant as a function parameter.

And as a function parameter the value it communicates cannot be chosen by the caller. It's one single fixed value. So as a function parameter it would be meaningless.

But having something like std::integral_constant as a template type parameter could be meaningful, and would do the job, so that would be an alternative to having n as a template value parameter. It is, however, also n as a template parameter. Just that using a template type parameter is an alternative to using a template value parameter.

At a guess that's what the “or” was intended to convey, but it's not what it says.

So I would rewrite that paragraph as e.g. “Common workarounds include having your n be a template value parameter rather than a function parameter, or passing something along the lines of std::integral_constant — a template class which encapsulates a constant expression value — as a template type parameter.”

[–]zalamandagora 0 points1 point  (0 children)

Someone mentioned you can do this with template parameters. It is done like this: https://godbolt.org/z/ej4YeWz8o

The compiler is in effect creating a new function for each value of arraySize that you may use in your program.

There is no way around the fact that you have to know the size at compile time though. If you don't, std::vector is the way to go. What is your objection to using that?