Does ScopeGuard use really lead to better code?

前端 未结 8 1901
-上瘾入骨i
-上瘾入骨i 2020-12-04 15:36

I came across this article written by Andrei Alexandrescu and Petru Marginean many years ago, which presents and discusses a utility class called ScopeGuard for writing exce

8条回答
  •  囚心锁ツ
    2020-12-04 16:23

    I often use it for guarding memory usage, things that need to be freed that were returned from the OS. For example:

    DATA_BLOB blobIn, blobOut;
    blobIn.pbData=const_cast(data);
    blobIn.cbData=length;
    
    CryptUnprotectData(&blobIn, NULL, NULL, NULL, NULL, CRYPTPROTECT_UI_FORBIDDEN, &blobOut);
    Guard guardBlob=guardFn(::LocalFree, blobOut.pbData);
    // do stuff with blobOut.pbData
    

提交回复
热议问题