简体   繁体   English

在C ++中澄清[[pure]]

[英]Clarification of [[pure]] in C++

Reading the most recent proposal for a standard C++ attribute [[pure]] I was left with a number of questions: 阅读标准C ++属性[[pure]]的最新提议,我留下了许多问题:

  1. Could a [[pure]] function read const globals? 一个[[pure]]函数可以读取const全局吗?
  2. Could a [[pure]] function read static const variables? 一个[[pure]]函数可以读取静态const变量吗?
  3. Could a [[pure]] function write but not read pointer or reference variables (and still return void possibly)? 一个[[pure]]函数可以写但不能读取指针或引用变量(并且仍然可能返回void)?

[[pure]], If I recall right, needs to be 100% certain that a particular input should always return a specific output. [[pure]],如果我没记错,需要100%确定特定输入应该始终返回特定输出。 Considering a const globals and static const globals should never change states, it should be ok. 考虑一个const全局变量和静态const全局变量应该永远不会改变状态,它应该没问题。 Finally, reading a pointer that could change states would not be a pure function. 最后,读取可能改变状态的指针不是一个纯函数。 If you write to pointers accessed elsewhere, then it has a side effect and is not [[pure]]. 如果你写入其他地方访问的指针,那么它有副作用,而不是[[纯]]。

You have to ask yourself if the function result is dependent on an extraneous variables' state, and if you answer yes, it is not [[pure]]. 你必须问自己函数结果是否依赖于一个无关变量的状态,如果你回答是,那就不是[[pure]]。 You also have to ask yourself if an input can have an effect on anything other than it's return value. 您还必须问自己输入是否会对除返回值以外的任何内容产生影响。 If that is true, then it is not [[pure]] 如果这是真的,那就不是[[纯]]

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

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