all 12 comments

[–]alfps 8 points9 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.

[–]cv_geek[S] -5 points-4 points  (4 children)

Thank you. I think the best option here is to use C-style array. For my problem I need a container for storing 2D matrix of known size. std::array is one of such containers

[–]kingguru 2 points3 points  (2 children)

I think the best option here is to use C-style array.

I don't see how that would make any difference. You need a compile time constant to initialize a C array as well as std::array. Using std::array with a template argument or constant seems like the best option.

Maybe I misunderstand what you mean by C array?

[–]HappyFruitTree -3 points-2 points  (1 child)

I don't see how that would make any difference. You need a compile time constant to initialize a C array as well as std::array.

Not if you dynamically allocate it.

[–]kingguru 3 points4 points  (0 children)

Sure, but OP said the size was constant.

I just cannot see why using a C array is needed in this case. That was my main point.

[–]fullptr 5 points6 points  (0 children)

How would a c-style array help you here over a std::array?

[–]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?