Using const int as array size

前端 未结 5 698
太阳男子
太阳男子 2020-12-21 18:59

Why am I able to use a locally declared const int as the size of an array declaration but am not allowed to do the same with a const int passed as

5条回答
  •  佛祖请我去吃肉
    2020-12-21 20:04

    In f2 the constant dim is known at compile time, in f1 it's not.

    And since C++ doesn't have variable-length arrays the dimensions of an array must be known at compile time.

    The use of const for function arguments is more of a hint to the compiler that the function will not modify the argument, allowing the compiler to for example make optimizations based on that information.

提交回复
热议问题