It fails on line 8 with message: `some_const2' cannot appear in a constant-expression . How can I cast some_var2 (or some_const2 maybe?) into a const type? Or if it's not possible, what workaround would you suggest?
I wouldn't recommend 2d arrays in C arrays, in C [][] means ** which means that every row points to another random spot on the heap, it is not 1 continuous block of memory.
It is incredibly easy to use use 1d arrays as 2d arrays (its even in this websites tutorial), and it makes it easier to move from C to vectors if you ever wanted to (vectors of vectors is something that should be avoided if possible because it is very ugly).
Also read up on your tutorials, books, and even practical examples of code, because shortint is not something that people would write, and const is something that absolutely cannot change (in its lifetime, so if you had the values put into the constructor, you could use the member initializer list IE: A::A(Value value) : const_value(value){} list to set them).
auto some_var1 = 20;
auto some_var2 = 30;
constauto some_const1 = 5;
constauto some_const2 = some_var2;
auto str1= newchar[some_var1][some_const1];
auto str2 = newchar[some_var2][some_const2];
I still get a similar error at line 8: [Error] the value of 'some_const2' is not usable in a constant expression. I can't hard type the value of some_var2 because in my actual program, the value would be returned by a function. However I can't always return const type from my function. That's the reason I'm asking for a workaround (if there is any).
standard c++ demands that const2 be known at compile time (because it is being used as an array dimension below).
auto is NOT a const, it is a variable, and while we all know it has a value of 30, the compiler is not as forgiving. The compiler wants a true compile time constant.
you MUST either do this:
const auto some_const2 = 30;
or this
const auto some_var2 = 30;
const auto some_const1 = 5;
const auto some_const2 = some_var2;
or some other combination of code that ensures a true const value
However I can't always return const type from my function. That's the reason I'm asking for a workaround (if there is any).
I had to check this myself
1 2 3 4 5
int test()
{
constint i = 51;
return i;
}
nope it works *shrugs*, unless you were to return a reference (why).
You nee to show us a minimal example of all your code, because we might be able to help you with an alternative solution (probably using c++ stl).
wow I am really out of touch.
But yea, if you physically cannot return a const from a function that is a constant expression found in compile time, then you must use pointer to pointers or the 1d array idea.
rog.cc:9:62: error: array size in new-expression must be constant
9 | char (*str2)[some_const2] = new char[some_var2][some_const2];
| ^
prog.cc:9:62: error: the value of 'some_const2' is not usable in a constant expression
prog.cc:6:13: note: 'some_const2' was not initialized with a constant expression
6 | const int some_const2 = some_var2;
| ^~~~~~~~~~~
as this compiler points out, because "some_const2" is not initialized with a constant expression, it's not a constant expression itself. You can make it so by making "some_var2" a constant expression: constshortint some_var2 = 30; (or what jonnin did above)
demo: https://wandbox.org/permlink/piTnQoNKXhmGBIpk