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