C++ 用不同的值初始化const容器的正确方法是什么?

C++ 用不同的值初始化const容器的正确方法是什么?,c++,c++11,initialization,constants,C++,C++11,Initialization,Constants,我有一些容器struct,其中包含一组配置元素 struct config { const std::vector<int> config_items; const std::vector<double> another_items; } 所有向量s'项都应该是常量,因为它们应该定义一次,并且永远不会更改 由于向量是常量,因此我只能在构造函数初始值设定项列表中初始化它们。但我希望有多个具有不同值的实例 我可以创建一些子structs来创建子构造函数中的每

我有一些容器
struct
,其中包含一组配置元素

struct config
{
    const std::vector<int> config_items;
    const std::vector<double> another_items;
}
所有
向量
s'项都应该是
常量
,因为它们应该定义一次,并且永远不会更改

由于
向量
常量
,因此我只能在构造函数初始值设定项列表中初始化它们。但我希望有多个具有不同值的实例

我可以创建一些子
struct
s来创建子构造函数中的每个配置。但这不起作用,因为我无法在子构造函数中初始化父成员:

struct config_1 : public config
{
    config_1() : config_items { 1, 2 }, another_items { 1.0, 2.0 } {} // Doesn't work
}
这似乎也是一个非常糟糕的决定(删除
const
,复制…):

背景:我想为我的嵌入式应用程序提供一个硬编码的
const
config结构(可能放在数据部分甚至闪存中)。无需从任何配置文件等读取内容

因此,我的问题是:您建议我如何创建这样一个
const
配置容器


编辑 这里的std::vector实际上是错误的。我正在使用一个自定义容器来保存实例中的数据,比如
std::array
,而不是
std::vector
,它在堆上分配存储

所以环境应该是这样的:

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

struct setup
{
    const std::array<config, 3> items;
}
struct config
{
    const std::vector<int> config_items = []{
        std::vector<int> retval;
        //put whatever stuff you want in retval
        return retval;
    }();
}
struct-config
{
const std::数组配置项;
const std::数组另一个\u项;
}
结构设置
{
常量std::数组项;
}

您可以执行以下操作:

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

struct setup
{
    const std::array<config, 3> items;
}
struct config
{
    const std::vector<int> config_items = []{
        std::vector<int> retval;
        //put whatever stuff you want in retval
        return retval;
    }();
}
struct-config
{
const std::vector config_items=[]{
std::向量检索;
//把你想要的东西放进retval
返回返回;
}();
}
优化编译器应该优化lambda,只需将适当的值放入向量中,这取决于您使它变得多么复杂以及调用了多少函数,但您可能应该仔细检查


请注意,
std::vector
将使用堆内存,因此数据将永远不会进入闪存(除了极为激进的编译器发挥神奇作用之外),因此您可能需要一个
std::array

可能只是使用这样的结构

struct config
{
    const std::vector<int> _a;
    config(const std::vector<int> &a): _a(a) {}
};
好的,让我们获取完整的样本:

    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}));
struct-config
{
常数std::向量_项;
配置(const std::vector&items):\u items(items){}
};
std::向量设置;
设置.emplace_back(配置({1,2}));
设置.emplace_back(配置({3,4}));

经过一些研究,我决定回答我自己的问题

正如前面提到的NathanOlivernwp一样,讨论中使用的
std::vector
s在堆上分配内存。实际上,我正在使用一个类似于
std::array
的定制容器,它将数据保存在实例本身中

所以我应该在问题中写下这样的话:

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

struct setup
{
    const std::array<config, 3> items;
}

现在,我有一个初始化的
实例
结构,其成员是
const
,没有运行时代码进行初始化(没有构造函数,没有方法调用),而是原始数据将直接存储在数据部分(默认情况下)。这正是我所需要的。

[FYI]向量是动态的,因此需要在运行时初始化它们。如果你想要静态数据,那么你应该考虑使用<代码> STD::Reals。@ NathanOliver,谢谢这个建议。code>vectors只是为了简单起见,我们使用自己的带有内部数据存储的模板容器。但我不明白:如何创建3个
config
实例,其中包含不同的
config\u项
?@dymanoid您需要返回一个配置,而不仅仅是vector。您是否考虑过使用包含非
const
向量的
const配置
    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}));
struct config
{
    const std::array<int, 2> config_items;
    const std::array<double, 2> another_items;
}

struct setup
{
    const std::array<config, 3> items;
}
setup instance
{
    // std::array c++11-ish double-braces aggregate initialization
    // this is the items member
    { {
        {
            // this is the config_items member
            { { 1, 2 } },

            // this is the another_items member
            { { 1.0, 2.0 } },
        },

        {
            { { 3, 4 } },
            { { 3.0, 4.0 } },
        },

        {
            { { 5, 6 } },
            { { 5.0, 6.0 } },
        }
    } }
};