Search code examples
c++classstdarray

How to turn a variable content constant for use it as the size of std::array?


I have a size variable that belongs to the class. I would like to use it as the size of a std::array, but I was not able to do this. I found some articles that mention constexpr, but nothing useful so far. Can you help me?

#include<array>
#include<iostream>

class MyClass{
private:
        int size; //variable I need to copy the content and make it constant.
        void calculateSize(int x){
                size = 2 * x;
        }

public:
        MyClass(){}

        void createArray(int val){

                calculateSize(val);
                std::cout << "the size is: " << size << std::endl;
                std::array<int, size> myArray; // error
        }
};

int main(){

        MyClass c;
        c.createArray(5);
        return 0;
}

The error:

main.cpp: In member function ‘void MyClass::createArray(int)’: main.cpp:20:19: error: use of ‘this’ in a constant expression std::array myArray;


Solution

  • The problem here is a misunderstanding of what constant means. In C++, std::array's must be of constant size, where constant means "the size is known at compile-time." As your class suggests, the size variable is calculated at runtime. Likewise, the constexpr keyword can only be used for a variable whose value is known at compile time and will not ever change.

    So you have a couple options.

    1. You can use a std::vector and initialize it with a size

      std::vector<int> vec(5); // Initializes a vector of size 5
      
    2. If you really do know the size of the array at compile time, you can use constexpr

      constexpr int size = 2 * 10;
      std::array<int, size> arr; // Make an array of size 20