本文介绍了constexpr的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更多是一个哲学问题,而不是实用的代码片段,但是也许C ++专家可以启发我(如果已经问过了,请道歉)。

This is more of a philosophical question rather than practical code snippet, but perhaps C++ gurus can enlighten me (and apologies if it's been asked already).

我有一直在阅读Meyers的有效的现代C ++书中的第15条,以及以下主题:(加上合理数量的谷歌搜索)。该项目超过了 constexpr 用于表达式的用途,即它定义了可以在给定编译时间输入的情况下返回编译时间值的函数。
而且,我所指的StackOverflow线程表明,某些编译器完全能够自己找出在编译时已知哪些函数调用结果。

I have been reading Item 15 in Meyers's "Effective Modern C++" book, as well as this thread: implicit constexpr? (plus a reasonable amount of googling). The item goes over usage of constexpr for expressions, namely that it defines functions that can return compile time values given compile time inputs.Moreover, the StackOverflow thread I referred to shows that some compilers are perfectly capable of figuring out for themselves which function invocation results are known at compile time.

因此产生了一个问题:与定义编译器何时应派生并允许静态的相比,为什么将 constexpr 添加到标准中/编译时值?

Hence the question: why was constexpr added to the standard as compared to defining when compilers should derive and allow static/compile-time values?

我意识到它使各种仅编译(例如 std :: array< T,constexpr> )的定义减少了可预测的,但是另一方面,按照Meyers的书, constexpr是接口的一部分,...,如果将其删除,则可能会导致任意数量的客户端代码停止编译
因此,不仅拥有明确的 constexpr 要求人们记住添加它,而且还向界面添加了永久语义。

I realise it makes various compile-only (e.g. std::array<T, constexpr>) definitions less predictable, but on the other hand, as per Meyers's book, constexpr is a part of the interface,..., if you remove it, you may cause arbitrarily large amounts of client code to stop compiling. So, not only having explicit constexpr requires people to remember adding it, it also adds permanent semantics to the interface.

说明:这个问题不是关于为什么要使用 constexpr 的问题。我很欣赏具有以编程方式导出编译时值的功能非常有用,并且在很多情况下自己使用了它。这是一个问题,为什么在编译器可以自行推断常量时间行为的情况下强制执行。

Clarification: This question is not about why constexpr should be used. I appreciate that having an ability to programatically derive compile-time values is very useful, and employed it myself on a number of occasions. It's a question on why it is mandatory in situations where compiler may deduce const-time behaviour on its own.

澄清号。 2 :这是一个代码段,显示编译器不会自动推断出这种情况,在这种情况下,我使用了g ++。

Clarification no. 2: Here is a code snippet showing that compilers do not deduce that automatically, I've used g++ in this case.

#include <array>

size_t test()
  {
  return 42;
  }

int main()
  {
  auto i = test();
  std::array<int, i> arrayTst;
  arrayTst[1] = 20;
  return arrayTst[1];
  }

std :: array 声明未能编译,因为我没有将 test()定义为 constexpr ,这当然是按照标准进行的。如果标准不同,那么没有什么可以阻止gcc独立地指出 test()总是返回一个常数表达式。

std::array declaration fails to compile because I have not defined test() as constexpr, which is of course as per standard. If the standard were different, nothing would have prevented gcc from figuring out independently that test() always returns a constant expression.

这个问题不是问标准定义了什么,而是为什么标准是它的方式??

This question does not ask "what the standard defines", but rather "why the standard is the way it is"?

推荐答案

constexpr 之前,编译器有时可以找出编译时间常数并使用它。但是,程序员永远不知道何时会发生这种情况。

Before constexpr the compilers could sometimes figure out a compile time constant and use it. However, the programmer could never know when this would happen.

此后,如果表达式不是编译时间常数,程序员会立即得到通知,并且他或她意识到了需要修复它。

Afterwards, the programmer is immediately informed if an expression is not a compile time constant and he or she realizes the need to fix it.

这篇关于constexpr的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:03