繁体   English   中英

C ++编译时检查是否有副作用

[英]C++ Compile Time Check for Sideffects

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

提出任何提议以检查这些断言是否成立

没有实现效果推断或效果类型化的C ++编译器,因此充其量仅支持对纯度的特别检查。

对于效果打字的背景,我建议本·利普迈尔(Ben Lippmeier)的博士学位论文, 类型推断和不纯净世界的优化

暂无
暂无

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

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