2

一些编译器支持pure 和 const,但是有没有提供检查这些断言是否成立的提议?例如:

int global_value = 42;
const int global_value_const = 42;

int MyPureFunction  __attribute__ (( pure_check? )) (
  int input,
  int* input_ptr,
  const int* input_const_ptr,
  int foo& input_ref,
  const int& input_const_ref)
{
   int temporary = input;    // Valid, can read local but mutable state.

   global_value += temporary;        // Invalid, cannot mutate external state
   temporary += global_value;        // Invalid,  cannot read non-const global data.
   temporary += global_value_const;  // Valid, can read const global data.

   temporary += *input_ptr;        // Invalid, cannot derefernece non-const ptr.
   temporary += *input_const_ptr;  // Valid, can dereference a const ptr.
   temporary += input_ref;         // Invalid, cannot use non-const reference.
   temporary += foo->value;        // Valid, can reference a const reference.

   return temporary;     // Valid., if all invalid statements above are removed...
}
4

1 回答 1

5

做任何提议来检查这些断言是否成立

没有实现效果推断或效果类型的 C++ 编译器,因此充其量只能支持对纯度的临时检查。

关于效果类型的背景,我建议 Ben Lippmeier 的博士论文,Type Inference and Optimization for an Impure World

于 2012-12-26T12:53:23.163 回答