0

I have a class as follows :

class A
{    //private members here
public:
   // other public functions here
template<typename T>
void create(int& no) const  // this function creates a static array containing no number of 
                            // elements of type T
{
  T vars[no]; // first, int no is a variable, can i create a static array here using a variable?
              /* second, I also see that visual studio is telling me "Local variable is not 
               initialized, does it need to be initialized? i only intend to use it for primitive  
               types and std::string but say if i needed to use if for a class object does it need 
               to be initialized? */
} 

};

I know that int variables cannot be used to create static arrays like so:

int num=10;
int arr[var];  // error, cannot use a variable to create a static array, it has to be const int

Because the variable has to be const int.

But I've seen code in books where they write code like in create function where they use an int variable to create static arrays of type T. Is this even legal?

Also, why is the visual studio telling me Local variable is not initialized at the declaration of the array?

  • [Variable-length arrays](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) are a common compiler extension, they are not part of the C++ standard. – Nathan Pierson Dec 07 '21 at 17:47
  • Also when I try [this code](https://godbolt.org/z/GT9M863KM) myself, I get errors but _not_ a "local variable is not initialized" error. Maybe that error is being caused by some of the code that you've commented out? – Nathan Pierson Dec 07 '21 at 17:53
  • @NathanPierson None of this helps. I've read the reference you gave but not helpful. Can you open the question again please? – Pratap Biswakarma Dec 07 '21 at 18:36
  • I don't understand what questions you have that weren't answered by the duplicate. It answers why "code like in `create`" will sometimes work. Your second, unrelated question about uninitialized variables is unanswerable without more information. – Nathan Pierson Dec 07 '21 at 18:41
  • I don't think the `local variable is not initialized` is an error or a warning but three dots appear under the `vars` in `T vars[no];` line. I think it's more of a suggestion where visual studio is suggesting I write the code like so `T vars[no]{};` and then the dots dissapear. Can you comment why is that so? And also what would be the standard way to create a static array out of `int& no`? I'd appreciate it much! – Pratap Biswakarma Dec 07 '21 at 18:46
  • There is no standard way to create a static array out of an `int&`. You can create a static array from a constant expression, or you can create a dynamic array from a non-constant expression. You can also use a standard library container like `std::vector` instead of trying to do it yourself. Depending on what you actually want `A` to be and what you want `create` to do, different approaches may be possible. – Nathan Pierson Dec 07 '21 at 18:53
  • I will say that just from what's shown, the signature alone is a bit suspect. `create` is `const`, meaning it doesn't modify the `A` instance it's being called from. But it's also `void`, so it doesn't return anything. So what, exactly, do you expect to be able to _do_ with the array you're creating? – Nathan Pierson Dec 07 '21 at 18:54
  • I'm trying to write a template function that creates no number of variables of variable types to use them later. – Pratap Biswakarma Dec 07 '21 at 19:06
  • Basically assign different values later on to variables of `T vars[no]];` later. – Pratap Biswakarma Dec 07 '21 at 19:07
  • If "later" means "after `create` has returned", your design is fundamentally flawed. `vars` ceases to exist when `create` returns. If your function doesn't store it somewhere else, and you don't return it so someone else can decide where to store it, you're out of luck. But this is turning into a discussion of very fundamental C++ issues like what is variable lifetime, which is beyond the scope of SO's question-and-answer format. – Nathan Pierson Dec 07 '21 at 19:09
  • I have taken those issues you mentioned already into consideration. But thanks anyway! – Pratap Biswakarma Dec 07 '21 at 19:12

0 Answers0