Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to initialize const containers with different values?

I have some container struct that holds a set of configuration elements.

struct config
{
    const std::vector<int> config_items;
    const std::vector<double> another_items;
}

Also, I have a "container of containers" that should hold a known and limited number of instances of those configuration containers (e.g. 3). Each config instance should then have different ints and doubles in the corresponding vectors.

struct setup
{
    const std::vector<config> items;
}

All the vectors' items should be const, because they should be defined once and never change.

Since the vectors are const, I can only initialize them in the constructor initializer list. But I want to have multiple instances with different values.

I could create some child structs to create each configuration in the child constructors. But this doesn't work, because I cannot initialize a parent member in the child constructor:

struct config_1 : public config
{
    config_1() : config_items { 1, 2 }, another_items { 1.0, 2.0 } {} // Doesn't work
}

This seems to be a very poor decision too (remove const, make copies...):

struct config
{
    std::vector<int> config_items;    
    std::vector<double> another_items;    
}

struct setup
{
    std::vector<config> items;
}

void init()
{
    config c;
    c.config_items = { 1, 2 };
    c.another_items = { 1.0, 2.0 };

    setup s;
    s.items = { c };
}

I cannot make a single initializer-list constructor too, because I have multiple vectors:

struct config
{
    config(std::initializer_list<int> i, std::initializer_list<double> d); // No go
    std::vector<int> config_items;    
    std::vector<double> another_items;    
}

Background: I want to have a hardcoded const config structure (probably placed in the DATA section or even in the flash memory) for my embedded application. No need to read things from any config files etc.

So my question is: what would you suggest me how I should create such a const configuration container?


EDIT

The std::vectors are actually wrong here. I'm using a custom container that holds the data in the instance like std::array and not like std::vector which allocates the storage on the heap.

So the environment should rather look like this:

struct config
{
    const std::array<int, 2> config_items;
    const std::array<double, 2> another_items;
}

struct setup
{
    const std::array<config, 3> items;
}
like image 917
dymanoid Avatar asked Sep 01 '25 20:09

dymanoid


1 Answers

maybe simply just use a construction like this?

struct config
{
    const std::vector<int> _a;
    config(const std::vector<int> &a): _a(a) {}
};

later somewhere in a code:

config c1({1, 2, 3});
config c2({3, 4, 5, 6});

alright, let's get full sample:

    struct config
    {
        const std::vector<int> _items;
        config(const std::vector<int> &items): _items(items) {}
    };
    std::vector<config> settings;
    settings.emplace_back(config({1, 2}));
    settings.emplace_back(config({3, 4}));
like image 182
fgrdn Avatar answered Sep 03 '25 14:09

fgrdn