我可以得到一个C ++编译器在编译时实例化对象吗? [英] Can I get a C++ Compiler to instantiate objects at compile time?

查看:108
本文介绍了我可以得到一个C ++编译器在编译时实例化对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些代码,它们有非常大量的合理简单的对象,我希望他们在编译时创建。我认为编译器将能够做到这一点,但我还不能弄清楚如何。

I am writing some code that has a very large number of reasonably simple objects and I would like them the be created at compile time. I would think that a compiler would be able to do this, but I have not been able to figure out how.

请执行以下操作:

#include <stdio.h>

typedef struct data_s {
    int a;
    int b;
    char *c;
} info;

info list[] = {
    1, 2, "a",
    3, 4, "b",
};

main()
{
   int i;
   for (i = 0; i < sizeof(list)/sizeof(*list); i++) {
     printf("%d %s\n", i, list[i].c);
   }
}

使用#C ++ *每个对象都有它的构造函数

Using #C++* each object has it constructor called rather than just being layed out in memory.

#include <iostream>
using std::cout;
using std::endl;

class Info {
    const int a;
    const int b;
    const char *c;
public:
    Info(const int, const int, const char *);
    const int get_a() { return a; };
    const int get_b() { return b; };
    const char *get_c() const { return c; };
};

Info::Info(const int a, const int b, const char *c) : a(a), b(b), c(c) {};

Info list[] = {
    Info(1, 2, "a"),
    Info(3, 4, "b"),
};

main()
{
    for (int i = 0; i < sizeof(list)/sizeof(*list); i++) {
        cout << i << " " << list[i].get_c() << endl;
    }
}

我只是看不到什么信息

推荐答案

在C ++ 2011中,你可以在编译器中完全实例化这些对象,在编译时创建对象。为此,你需要做各种常量表达式,但是:

In C++ 2011 you can create objects at compile time. For this to happen, you need to make various things constant expressions, however:


  1. 构造函数需要声明 constexpr

  2. 您声明的实体需要声明为 constexpr

  1. The constructor needs to be declared constexpr.
  2. The entity you declare needs to be declared constexpr.

注意,几乎所有的 const 限定符都是不相关的或者位置错误。下面是一个使用各种校正的例子,并且实际上展示了在编译期间初始化 list 数组(通过使用它的成员来定义 enum ):

Note, that nearly all your const qualifiers are either irrelevant or in the wrong location. Here is an example with the various correction and also actually demonstrating that the list array is initialized during compile time (by using members of it to define the values of an enum):

#include <iostream>
#include <iterator>

class Info {
    int a;
    int b;
    char const*c;

public:
    constexpr Info(int, int, char const*);
    constexpr int get_a() const { return a; }
    constexpr int get_b() const { return b; }
    constexpr char const*get_c() const { return c; }
};

constexpr Info::Info(int a, int b, char const*c)
  : a(a), b(b), c(c) {}

constexpr Info list[] = {
    Info(1, 2, "a"),
    Info(3, 4, "b"),
};

enum {
    b0 = list[0].get_b(),
    b1 = list[1].get_b()
};

int main()
{
    std::cout << "b0=" << b0 << " b1=" << b1 << "\n";
    for (Info const* it(list), *end(list); it != end; ++it) {
        std::cout << (it - list) << " " << it->get_c() << "\n";
    }
}

这篇关于我可以得到一个C ++编译器在编译时实例化对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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