简体   繁体   中英

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. Those constexpr rules are complex and far to long to put in a SO answer; find a good C++ book.

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++ specifies the behaviour of the program running on an abstract machine, not what actually happens on the hardware.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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