0

Can we init a static array without using template class?

Something look like:

struct Foo
{
public:
    static size_t Size;
    static std::array<int, Size> arr;
    static void init(size_t Size)
    {
        // Do init method with Size
    }
};

int main()
{
    Foo::init(10);
}

Currently I'm using the template, but that's not my expectation.

template <size_t Size>
struct Foo
{
public:
    static std::array<int, Size>;
};
template <size_t Size>
std::array<int, Size> arr;
icarius
  • 1
  • 1
  • Your question is probably missing requirements, but let's start by looking at a missing detail. In your "Something look like" code, what is `Size` supposed to be in the declaration of `arr`? There is no declaration of `Size` before that line, so `Size` is an unrecognized identifier (a.k.a. "meaningless") at that point. How do you plan to tell the compiler how big to make `arr`? – JaMiT Feb 05 '21 at 06:25
  • My last purpose is ```Foo::init(10)``` can be able to use, this will create array inside Foo, not a vector, that store my specific Object. I dont want to use Foo<10> because of in my real application, I need the template for other type. – icarius Feb 05 '21 at 06:55
  • 1
    the Size template of array needs to be a compile time constant. nothing more. You simply can't expect array to have different sizes for different instances of Foo unless Foo is a template. – Raildex Feb 05 '21 at 07:00
  • Do these answer your question? [Initializing std::array inside a function](https://stackoverflow.com/questions/26775561/initializing-stdarray-inside-a-function) or [How to turn a variable content constant for use it as the size of std::array?](https://stackoverflow.com/questions/59572091/how-to-turn-a-variable-content-constant-for-use-it-as-the-size-of-stdarray) or [Can the dimension of std::array be read from command line arguments?](https://stackoverflow.com/questions/64537609/can-the-dimension-of-stdarray-be-read-from-command-line-arguments) – JaMiT Feb 05 '21 at 07:13

0 Answers0