简体   繁体   English

C ++中的运行时运算符

[英]Run-time operators in C++

What is the definition of compile-time and run-time operator in C++? C ++中编译时和运行时运算符的定义是什么?

I know that sizeof() is a compile-time operator in C++ but which are the run-time operators? 我知道sizeof()是C ++中的编译sizeof()算符,但它是运行时运算符?


( Originally posted for 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.) 最初bc90 发布 ;我不想浪费我发布的答案,之后很明显他只想要一个C答案。嘿,也许这有助于“澄清”某人的“疑惑”。)

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. 显而易见的是sizeof ,在程序执行之前永远不需要推迟。 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. 作为另一个例子, const_cast绝对没有理由具有任何运行时相关性,因为在编译的程序中甚至不存在const A static_cast may be optimised away, but that would depend on what the relevant conversion operators do. static_cast 可能会被优化掉,但这取决于相关转换运算符的作用。

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"? 但是这会使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. 如果适用的constexpr规则适用,则函数调用operator ()只能在编译时调用,或者函数定义在与调用站点相同的转换单元中可见并且非常简单。

Whilst we're at it, reinterpret_cast disqualifies an expression from being constexpr . 虽然我们正处于这种状态,但reinterpret_cast取消了表达不被constexpr资格。 This is reasonably unintuitive but, according to the rationale in CWG issue #1384 : 这是相当不直观的,但根据CWG问题#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. 虽然在C ++ 03中的地址常量表达式中允许使用reinterpret_cast ,但是这种限制已在某些编译器中实现,并且未证明会破坏大量代码。 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. CWG认为处理其tpes [sic]改变的指针(指针算术和取消引用不允许在这样的指针上)的复杂性超过了放宽当前限制的可能效用。

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. 我的观点是,实际上,不值得尝试为此提出一般规则:C ++程序编译的过程并不是那么简单。 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. 为此,你有我的同情心。

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

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