C ++编译时间检查副作用

时间:2012-12-25 17:57:02

标签: c++ functional-programming purely-functional

有些编译器支持pure and 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...
}

1 个答案:

答案 0 :(得分:5)

  

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

没有C ++编译器可以实现效果推理或效果输入,因此最多只支持对纯度的临时检查。

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