Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run-time operators in C++

Tags:

c++

What is the definition of compile-time and run-time operator in C++?

I know that sizeof() is a compile-time operator in C++ but which are the run-time operators?


(Originally posted for cc++ by bc90; I did not wish to waste the answer that I posted before it became clear he only wanted a C answer. Hey, perhaps this can help "clarify" someone's "doubts" one day.)

like image 463
Lightness Races in Orbit Avatar asked May 11 '15 10:05

Lightness Races in Orbit


People also ask

What is run time in C?

Runtime is a piece of code that implements portions of a programming language's execution model. In doing this, it allows the program to interact with the computing resources it needs to work. Runtimes are often integral parts of the programming language and don't need to be installed separately.

What is compile time operator in C?

Sizeof is a much used operator in the C or C++. It is a compile time unary operator which can be used to compute the size of its operand. The result of sizeof is of unsigned integral type which is usually denoted by size_t.

What is run time and compile time example?

Compile time is the period when the programming code (such as C#, Java, C, Python) is converted to the machine code (i.e. binary code). Runtime is the period of time when a program is running and generally occurs after compile time.

What is build time and run time?

Compile-time and Runtime are the two programming terms used in the software development. Compile-time is the time at which the source code is converted into an executable code while the run time is the time at which the executable code is started running.


1 Answers

It's not as simple as that. There is no "list of runtime operators" or "list of compile-time operators". There are operators, and many of them may be optimised to be "executed" by the compiler rather than coded into the program as an operation to be performed at runtime.

The obvious one is sizeof, which never needs to be deferred until program execution. In fact, speaking generally, operators which act on types may be expected to be purely a semantic operation with no reliance on run-time information. As another example, there is absolutely no reason for const_cast to have any run-time relevance, seeing as const doesn't even exist in your compiled program. A static_cast may be optimised away, but that would depend on what the relevant conversion operators do.

What may not be so obvious is that many expressions that invoke, say, arithmetic operators, may be optimised away.

For example:

const int x = 2 + 4;

That addition is not going to be performed at runtime unless your compiler is very, very silly. But does that make operator+ a "compile-time operator"? No.

And the function call operator () is only ever going to be invokable at compile-time if the applicable constexpr rules apply, or the function definition is visible in the same translation unit as the call site and is sufficiently trivial.

Whilst we're at it, reinterpret_cast disqualifies an expression from being constexpr. This is reasonably unintuitive but, according to the rationale in CWG issue #1384:

Although reinterpret_cast was permitted in address constant expressions in C++03, this restriction has been implemented in some compilers and has not proved to break significant amounts of code. CWG deemed that the complications of dealing with pointers whose tpes [sic] changed (pointer arithmetic and dereference could not be permitted on such pointers) outweighed the possible utility of relaxing the current restriction.

My point is that, really, it's not worth trying to come up with general rules for this: the process of C++ program compilation is just not that straightforward. You have to examine what your program is doing on a case-by-case basis.

However, I do appreciate that you are trying to answer a poor exam question. For that, you have my sympathies.

like image 163
Lightness Races in Orbit Avatar answered Oct 14 '22 06:10

Lightness Races in Orbit