Pergunta

Some compilers support pure and const, but do any offer to check that these assertions hold? For example:

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...
}
Foi útil?

Solução

do any offer to check that these assertions hold

There are no C++ compilers that implement effect inference or effect typing, so only ad hoc checks for purity would be supported, at best.

For background on effect typing, I suggest Ben Lippmeier's PhD thesis, Type Inference and Optimisation for an Impure World

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top