2

This is a C++ programming code to display the values of array1 and array2 but I am getting a compile time error as 'Constant Expression Required'. Please Help

void display(const int const1 = 5)
{
    const int const2 = 5;
    int array1[const1];
    int array2[const2];

    for(int i = 1 ; i < 5 ; i++)
    {
        array1[i] = i;
        array2[i] = i * 10;

        std::cout << array1[i] << std::endl;
    }
}

void main()
{
    display(5);
}
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
Abhinav Kushagra
  • 176
  • 1
  • 2
  • 15
  • 3
    This is almost the same as: http://stackoverflow.com/questions/18996258/array-initialization-use-const-variable-in-c – NathanOliver Sep 30 '16 at 12:51
  • 2
    When posting question regarding build errors, always include the complete error output, in full and unedited. Please edit your question to include that. – Some programmer dude Sep 30 '16 at 12:51
  • 1
    However, note that C++ doesn't support [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array) and that `const1` is not a compile-time constant. – Some programmer dude Sep 30 '16 at 12:51
  • As well as all the above comments, consider using `std::vector`. – Bathsheba Sep 30 '16 at 12:53
  • How is it not a compile-time constant? @Joachim Pileborg – Abhinav Kushagra Sep 30 '16 at 12:58
  • Its a function parameter. You can call that function with a run time value. – NathanOliver Sep 30 '16 at 13:01
  • 1
    Constant arguments are only constant in the meaning that they can not be modified inside the functions. They are not compile-time constants that are set at the time of compilation. Having the compiler figuring out that you are actually calling the function with a compile-time constant like `5` is very hard, and not a requirement. All the compiler knows is that the function can be called with any value at any time during run-time, and that the value could just as easily come from a variable that is initialized at run-time. – Some programmer dude Sep 30 '16 at 13:01
  • But I have assigned that value as constant, it means it wouldn't change its value. @NathanOliver – Abhinav Kushagra Sep 30 '16 at 13:08
  • @AbhinavKushagra Please read Joachim Pileborg's comment – NathanOliver Sep 30 '16 at 13:09
  • @JoachimPileborg Thanks... It really helped me to understand. – Abhinav Kushagra Sep 30 '16 at 13:11

1 Answers1

1

In C++, const is not always constexpr. Back in the days, constexpr didn't exist, so the only way of having a compile time constant was to either use const with a literal, or to use enum, because both of these are easy for the compiler to check the value.

However, in C++11, we added constexpr, which guaranties that a constexpr variable has a value available at compile-time, and state that constexpr function can be evaluated aat compile time if all arguments are constexpr too.

In your code, you can write your variable const2 like this:

void display(const int const1=5)
{
    constexpr int const2 = 5;

    // ...
}

Now your code is much more expressive about what you are doing. instead of relying that the const may be available at compile time, you say "this variable has a value known at compile time, here's the value".

However, if you try to change const1, you'll get an error. Parameters, even with default value always as a value known at runtime. If the value is only known at runtime, you can't use it in template parameters or array size.

If you want your function to be able to receive the value const1 as a constant expression from where you can receive it as a template parameter, since template parameters are always known at compile time.

template<int const1 = 5>
void display()
{
    constexpr int const2 = 5;

    int array1[const1];
    int array2[const2];
}

You will have to call your function like that:

// const1 is 5
display();

// const1 is 10
display<10>();

If you want to know more about templates, go check Function templates, or this tutorial

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • Can `display<10>();` change the value of **const1**?, as it is a constant and may show an error. @GuillaumeRacicot – Abhinav Kushagra Sep 30 '16 at 13:54
  • Not really. You can see it as *inside the function display<10>, the value of `const1` is 10*. For integral values, template parameter are working in a similar matter than normal parameter, except that they need to be known at compile time. If you meant *"will the value of `const1` inside `display<10>` will be 10?"* then the amswer is yes, the value you sent is indeed 10. – Guillaume Racicot Sep 30 '16 at 14:04
  • You see, a template will not be a single function with a single `const1` inside. Each `display<1>()` `display<2>()` `display<3>()` are different function, because it's a template. The compiler will create, from your template, a bunch of different function, each with their `const1` inside, each with a different value. – Guillaume Racicot Sep 30 '16 at 14:07
  • I added link in the answer. – Guillaume Racicot Sep 30 '16 at 14:08