It seems parameter passed in functions cannot maintain their const property. Say that I need to initialize a const variable using information from parameters within a function, then create an array type. How can I do?
example:
#include <array>
using namespace std;
void foo(const int info)
{
const int num = info + 1; // seems num cannot be constant
array<int, num> arr;
}
compile error:
test.cpp: In function ‘void foo(int)’:
test.cpp:8:16: error: the value of ‘num’ is not usable in a constant expression
array<int, num> arr;
^
test.cpp:7:15: note: ‘num’ was not initialized with a constant expression
const int num = info + 1; // seems num cannot be constant
^
Update: using array type would cause trouble like that, but using simple type array is just ok:
void foo(int info)
{
int array[info];
}
Isn't info
should be assigned during compile time?