简体   繁体   English

将 `const` 数组的元素设置为 c++ 中另一个数组的长度

[英]Set an element of a `const` array as the length of another array in c++

I defined a const int array like this:我定义了一个这样的const int数组:

const int arr[] = { 100 , 200, 300, 400 };

Now i want to set one of the elements of above array as the length of another array like the following:现在我想将上述数组的元素之一设置为另一个数组的长度,如下所示:

char buffer[arr[3]];

But it gave me a compile time error :但它给了我一个编译时error

 non-constant arguments or reference to a non-constant symbol

I studied this This question to solve my problem but i became confuse about these questions:我研究了这个这个问题来解决我的问题,但我对这些问题感到困惑:

  • Why can't i set an element of a const array as the length of another array?为什么我不能将const数组的元素设置为另一个数组的长度?
  • Are the elements of a const array constant or read only? const数组的元素是常量还是只读的?
  • What is the differences between a const and read only statements in c? c 中的const只读语句有什么区别?

Regards!问候!

There are really two different kinds of constant "things" in C++. C++ 中确实有两种不同的常量“事物”。

The one that you know as the const keyword: you can't modify it at runtime .你知道的那个const关键字:你不能在运行时修改它。

And the one that's known as a constant value to the compiler at compile time .并且在编译时被编译器称为常量值。

That one would be:那将是:

constexpr int arr[] = { 100 , 200, 300, 400 };

C++ requires an array size to be a constexpr expression, and not just a const one. C++ 要求数组大小是constexpr表达式,而不仅仅是const表达式。 Some compilers let you get away with just a const size (and not even that, actually), but that's not the current C++ standard.一些编译器让您只需要一个const大小(实际上甚至没有),但这不是当前的 C++ 标准。

You might be wondering why, in this case, this is not a constant value at compile time.您可能想知道为什么在这种情况下,这不是编译时的常量值。 After all: it's right there.毕竟:它就在那里。 It's three digits.是三位数。 An integer. integer。 It can't go anywhere.它不能在任何地方 go 。

Well, that would be a different, pedantic question, but is mostly irrelevant.好吧,那将是一个不同的、迂腐的问题,但大多数情况下是无关紧要的。 Your compiler is well within its rights to reject a non- constexpr expression in this case, as ill-formed.在这种情况下,您的编译器完全有权拒绝非constexpr表达式,因为它的格式不正确。 So it does.确实如此。 And you have no choice but to obey your compiler's demands.你别无选择,只能服从编译器的要求。

I realized const and constexpr in the following statements:我在以下语句中实现了constconstexpr

  • const : To be evaluated at runtime and it's accepted by compiler and can't change in runtime. const :在运行时被评估并且被编译器接受并且不能在运行时改变。

     const int a = std::cin.get(); // correct const int b = 5; // correct
  • constexpr : To be evaluate at compile time constexpr :在编译时进行评估

    constexpr int b = std::cin.get(); // incorrect (because it evaluate in compile time, so compiler cannot forecast the value at compile time) constexpr int b = 65; // correct

Now in my code as i understood, i think the char buffer is evaluate the array size at compile time and const int arr will be avaluate at runtime .现在在我的code中,我认为char buffer编译时评估数组大小,而const int arr将在运行时评估。 so cannot set char buffer array length with a number that will be evaluate at runtime and we need a constant value.因此无法使用将在运行时评估的数字设置char buffer数组长度,我们需要一个量值。

Notice:注意:

const int arr[] = { 100 , 200, 300, 400 };     // Evaluate at runtime
char buffer[arr[3]];                           // Evaluate at compile time and cause error

So we need a const number that evaluated at compile time to set array length of the char buffer :所以我们需要一个在编译时评估的const数字来设置char buffer的数组长度:

constexpr int arr[] = { 100 , 200, 300, 400 };     // Evaluate at compile time
char buffer[arr[3]];                               // Evaluate at compile time

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM