简体   繁体   English

编译时间常数确定

[英]Compile time constant determination

How do you know whether something is a constant at compile time? 您如何知道某个东西在编译时是否为常数?

Is there a general rule? 有一般规则吗?

For example virtual calls, are they known at compile time? 例如虚拟调用,它们在编译时是否已知?

Would it correct to say that anything that requires heap memory allocation will not be known at compile time, because memory is only assigned at runtime. 可以说在编译时不会知道需要堆内存分配的任何内容,因为仅在运行时才分配内存,这是否正确? Only to accessing this memory and its value can an object its referencing be known 只有访问该内存及其值,才能知道其引用的对象

There are piles of ways, no general rule, and virtual calls are generally not unless the compiler "devirtualizes" them. 有很多方法,没有通用规则,除非编译器将它们虚拟化,否则虚拟调用通常不会。

Under the "as if" rule anything the compiler can solve at compile time can be done at compile time. 根据“好像”规则,编译器可以在编译时解决的任何事情都可以在编译时完成。

In addition, certain operations are difficult not to do at compile time, like evaluate non-type template parameters. 此外,某些操作很难在编译时执行,例如评估非类型模板参数。 The C++ standard has constexpr rules that dictate what kind of operations compilers must permit when initializing non-type template parameters. C ++标准具有constexpr规则,这些规则指示编译器在初始化非类型模板参数时必须允许哪种操作。 Those constexpr rules are complex and far to long to put in a SO answer; 这些constexpr规则很复杂,要提出一个SO答案constexpr很长时间。 find a good C++ book. 找到一本不错的C ++书。

But it is important to know that as-if goes both ways. 但是重要的是要知道两种情况都是可行的。 Compilers are free to do anything at runtime; 编译器可以在运行时自由执行任何操作; a C++ interpreter can be a conforming C++ compiler. C ++解释器可以是合格的C ++编译器。

C++ specifies the behaviour of the program running on an abstract machine, not what actually happens on the hardware. C ++指定在抽象计算机上运行的程序的行为,而不是硬件上实际发生的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM