如何在C ++中编码大型复杂的常量数据结构 [英] How to encode large complex, constant data structures in C++

查看:153
本文介绍了如何在C ++中编码大型复杂的常量数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去,我使用gcc的 C99风格的复合文字扩展名来C ++在代码中编码嵌套的常量数据结构。这里有一个例子:

  #include< iostream> 
使用namespace std;

结构树{
const char * name;
const Tree * left;
const树*右;
};
$ b const Tree * const tree =(Tree []){
top,// name
(Tree []){
left,
0,
0
},
(Tree []){
right,
0,
0
}
};

static void dump(const Tree * tree){
if(!tree){
cout<< 空值;
return;
}

cout<<树 - >名称<< (;
dump(tree-> left);
cout<<,;
dump(tree-> right);
cout< <);
}

int main(void){
dump(tree);
cout<< \\\
;
}

这个想法是为这些相当大的常量结构使用静态存储持续时间,零初始化成本,实际上,除非需要,否则不需要将任何内容分页到内存中。



然而,最近版本的clang不再适用,最新的OS X捆绑铿锵的名字'gcc'。所以我需要一个不同的解决方案。



我在C ++中使用的最符合标准的习惯用法是什么?

I并不是特别想支付在这些结构中构建所有对象的代价,所以如果能够避免这种情况,那就太好了。

解决方案 C ++ 11 统一初始化语法应该可以工作:

  const Tree * const tree = new Tree {top,
new Tree {left,nullptr,nullptr},
new Tree {right,nullptr,nullptr}
};

否则,建立一个构造函数,将名称和子树作为参数。 b




如果您不希望动态分配结构,您必须自行创建每个结构,然后使用例如

 命名空间
{
const树leftTree {left,nullptr, nullptr};
const树rightTree {right,nullptr,nullptr};
const Tree topTree {top,& leftTree,& rightTree};
}

const Tree * const tree =& topTree;


In the past, I've used gcc's C99-style compound-literal extension to C++ to encode nested constant data structures in code. Here's an example:

#include <iostream>
using namespace std;

struct Tree {
    const char *name;
    const Tree *left;
    const Tree *right;
};

const Tree *const tree = (Tree []) {
    "top", // name
    (Tree[]) {
        "left",
        0,
        0
    },
    (Tree[]) {
        "right",
        0,
        0
    }
};

static void dump(const Tree *tree) {
    if (!tree) {
        cout << "null";
        return;
    }

    cout << tree->name << "(";
    dump(tree->left);
    cout << ", ";
    dump(tree->right);
    cout << ")";
}

int main(void) {
    dump(tree);
    cout << "\n";
}

The idea is to use static storage duration for these reasonably large constant structures, with zero initialization cost, and indeed no need to page anything into memory unless needed.

This no longer works in recent version of clang, however, and the latest OS X is bundling clang under the name 'gcc'. So I need a different solution.

What's the best standard-conforming idiom for this in C++?

I don't particularly want to pay the cost of constructing all the objects in these structures, so if that can be avoided, it would be great.

解决方案

C++11 uniform initialization syntax should work:

const Tree* const tree = new Tree{"top",
    new Tree{"left", nullptr, nullptr},
    new Tree{"right", nullptr, nullptr}
};

Otherwise, just make a constructor taking the name and subtrees as arguments.


If you don't want the structures to be dynamically allocated, you have to create each structure by itself, and then link them together using e.g. the address-of operator:

namespace
{
    const Tree leftTree{"left", nullptr, nullptr};
    const Tree rightTree{"right", nullptr, nullptr};
    const Tree topTree{"top", &leftTree, &rightTree};
}

const Tree* const tree = &topTree;

这篇关于如何在C ++中编码大型复杂的常量数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆