2017-04-04 2 views
0

에 대한 임 안드레이 알렉산드 레스 쿠와 페 트루 Marginean와 함께 연주는 scoped guard object안함 "을 사용하지 않는 변수"ScopedGuard

당신은 "사용되지 않는 변수"오류가 발생합니다.

ScopeGuard scope_guard = MakeGuard(&foo); 

단지

const ScopeGuardImplBase& scope_guard = ScopeGuardImpl0<void(*)()>(&foo); 

임은의 끝에 어떤 행동을 얻기 위해 매크로 사용하고 다음 코드는 LOKI

class ScopeGuardImplBase 
{ 
    ScopeGuardImplBase& operator =(const ScopeGuardImplBase&); 

protected: 

    ~ScopeGuardImplBase() 
    {} 

    ScopeGuardImplBase(const ScopeGuardImplBase& other) throw() 
     : dismissed_(other.dismissed_) 
    { 
     other.Dismiss(); 
    } 

    template <typename J> 
    static void SafeExecute(J& j) throw() 
    { 
     if (!j.dismissed_) 
      try 
      { 
       j.Execute(); 
      } 
      catch(...) 
      {} 
    } 

    mutable bool dismissed_; 

public: 
    ScopeGuardImplBase() throw() : dismissed_(false) 
    {} 

    void Dismiss() const throw() 
    { 
     dismissed_ = true; 
    } 
}; 

//////////////////////////////////////////////////////////////// 
/// 
/// \typedef typedef const ScopeGuardImplBase& ScopeGuard 
/// \ingroup ExceptionGroup 
/// 
/// See Andrei's and Petru Marginean's CUJ article 
/// http://www.cuj.com/documents/s=8000/cujcexp1812alexandr/alexandr.htm 
/// 
/// Changes to the original code by Joshua Lehrer: 
/// http://www.lehrerfamily.com/scopeguard.html 
//////////////////////////////////////////////////////////////// 

typedef const ScopeGuardImplBase& ScopeGuard; 

template <typename F> 
class ScopeGuardImpl0 : public ScopeGuardImplBase 
{ 
public: 
    static ScopeGuardImpl0<F> MakeGuard(F fun) 
    { 
     return ScopeGuardImpl0<F>(fun); 
    } 

    ~ScopeGuardImpl0() throw() 
    { 
     SafeExecute(*this); 
    } 

    void Execute() 
    { 
     fun_(); 
    } 

protected: 
    ScopeGuardImpl0(F fun) : fun_(fun) 
    {} 

    F fun_; 
}; 

template <typename F> 
inline ScopeGuardImpl0<F> MakeGuard(F fun) 
{ 
    return ScopeGuardImpl0<F>::MakeGuard(fun); 
} 

문제가 사용 함께에서 가져옵니다 대응 :

#define SCOPE_GUARD ScopedGuard scope_guard = MakeGuard 

이러한 방법으로, 사용자는 단지

SCOPE_GUARD(&foo, param) ... 

열심히하지 않는 경고를 해제하고이 매크로를 호출 할 수 있습니다.

다른 사람이 나를 더 잘 이해하도록 도와 줄 수 있습니까? 아니면 사용하지 않고 -wno-unused-variable을 사용하여 해결책을 제시 할 수 있습니까?

+1

가능한 복제 [? 범위 가드를 사용하는 경우 경고를 방지하는 방법 (http://stackoverflow.com/questions/35587076/how-to-avoid- 경고 - 사용 - 범위 - 가드) –

답변

0

당신은 기존의 방법을 시도 할 수 있습니다

(void)scope_guard; 
관련 문제