SafeInt 클래스

정수 오버플로 방지 하려면 정수 프리미티브를 확장 하 고 여러 종류의 정수를 비교 합니다.

template<typename T, typename E = _SAFEINT_DEFAULT_ERROR_POLICY>
class SafeInt;

매개 변수

템플릿

설명

T

형식을 정수 또는 부울 매개 변수는 SafeInt 대체 합니다.

E

열거형된 데이터 형식의 처리 정책 오류를 정의 합니다.

U

정수 또는 부울 매개 변수는 보조 피연산자에 대 한 형식입니다.

Parameter

설명

[in] rhs

여러 독립 실행형 함수에서 연산자의 오른쪽에 있는 값을 나타내는 입력된 매개 변수입니다.

[in] I

여러 독립 실행형 함수에서 연산자의 오른쪽에 있는 값을 나타내는 입력된 매개 변수입니다.

[in] 비트

여러 독립 실행형 함수에서 연산자의 오른쪽에 있는 값을 나타내는 입력된 매개 변수입니다.

Members

Dd570021.collapse_all(ko-kr,VS.110).gifPublic 생성자

Name

설명

SafeInt::SafeInt

기본 생성자입니다.

Dd570021.collapse_all(ko-kr,VS.110).gif할당 연산자

Name

구문

=

template<typename U>

SafeInt<T,E>& operator= (const U& rhs)

=

SafeInt<T,E>& operator= (const T& rhs) throw()

=

template<typename U>

SafeInt<T,E>& operator= (const SafeInt<U, E>& rhs)

=

SafeInt<T,E>& operator= (const SafeInt<T,E>& rhs) throw()

Dd570021.collapse_all(ko-kr,VS.110).gif캐스팅 연산자

Name

구문

bool

operator bool() throw()

char

operator char() const

signed char

operator signed char() const

unsigned char

operator unsigned char() const

__int16

operator __int16() const

unsigned __int16

operator unsigned __int16() const

__int32

operator __int32() const

unsigned __int32

operator unsigned __int32() const

long

operator long() const

unsigned long

operator unsigned long() const

__int64

operator __int64() const

unsigned __int64

operator unsigned __int64() const

wchar_t

operator wchar_t() const

Dd570021.collapse_all(ko-kr,VS.110).gif비교 연산자

Name

구문

<

template<typename U>

bool operator< (U rhs) const throw()

<

bool operator< (SafeInt<T,E> rhs) const throw()

>=

template<typename U>

bool operator>= (U rhs) const throw()

>=

Bool operator>= (SafeInt<T,E> rhs) const throw()

>

template<typename U>

bool operator> (U rhs) const throw()

>

Bool operator> (SafeInt<T,E> rhs) const throw()

<=

template<typename U>

bool operator<= (U rhs) const throw()

<=

bool operator<= (SafeInt<T,E> rhs) const throw()

==

template<typename U>

bool operator== (U rhs) const throw()

==

bool operator== (bool rhs) const throw()

==

bool operator== (SafeInt<T,E> rhs) const throw()

!=

template<typename U>

bool operator!= (U rhs) const throw()

!=

bool operator!= (bool b) const throw()

!=

bool operator!= (SafeInt<T,E> rhs) const throw()

Dd570021.collapse_all(ko-kr,VS.110).gif산술 연산자

Name

구문

+

const SafeInt<T,E>& operator+ () const throw()

-

SafeInt<T,E> operator- () const

++

SafeInt<T,E>& operator++ ()

--

SafeInt<T,E>& operator-- ()

%

template<typename U>

SafeInt<T,E> operator% (U rhs) const

%

SafeInt<T,E> operator% (SafeInt<T,E> rhs) const

%=

template<typename U>

SafeInt<T,E>& operator%= (U rhs)

%=

template<typename U>

SafeInt<T,E>& operator%= (SafeInt<U, E> rhs)

*

template<typename U>

SafeInt<T,E> operator* (U rhs) const

*

SafeInt<T,E> operator* (SafeInt<T,E> rhs) const

*=

SafeInt<T,E>& operator*= (SafeInt<T,E> rhs)

*=

template<typename U>

SafeInt<T,E>& operator*= (U rhs)

*=

template<typename U>

SafeInt<T,E>& operator*= (SafeInt<U, E> rhs)

/

template<typename U>

SafeInt<T,E> operator/ (U rhs) const

/

SafeInt<T,E> operator/ (SafeInt<T,E> rhs ) const

/=

SafeInt<T,E>& operator/= (SafeInt<T,E> i)

/=

template<typename U>

SafeInt<T,E>& operator/= (U i)

/=

template<typename U>

SafeInt<T,E>& operator/= (SafeInt<U, E> i)

+

SafeInt<T,E> operator+ (SafeInt<T,E> rhs) const

+

template<typename U>

SafeInt<T,E> operator+ (U rhs) const

+=

SafeInt<T,E>& operator+= (SafeInt<T,E> rhs)

+=

template<typename U>

SafeInt<T,E>& operator+= (U rhs)

+=

template<typename U>

SafeInt<T,E>& operator+= (SafeInt<U, E> rhs)

-

template<typename U>

SafeInt<T,E> operator- (U rhs) const

-

SafeInt<T,E> operator- (SafeInt<T,E> rhs) const

-=

SafeInt<T,E>& operator-= (SafeInt<T,E> rhs)

-=

template<typename U>

SafeInt<T,E>& operator-= (U rhs)

-=

template<typename U>

SafeInt<T,E>& operator-= (SafeInt<U, E> rhs)

Dd570021.collapse_all(ko-kr,VS.110).gif논리 연산자

Name

구문

!

bool operator !() const throw()

~

SafeInt<T,E> operator~ () const throw()

<<

template<typename U>

SafeInt<T,E> operator<< (U bits) const throw()

<<

template<typename U>

SafeInt<T,E> operator<< (SafeInt<U, E> bits) const throw()

<<=

template<typename U>

SafeInt<T,E>& operator<<= (U bits) throw()

<<=

template<typename U>

SafeInt<T,E>& operator<<= (SafeInt<U, E> bits) throw()

>>

template<typename U>

SafeInt<T,E> operator>> (U bits) const throw()

>>

template<typename U>

SafeInt<T,E> operator>> (SafeInt<U, E> bits) const throw()

>>=

template<typename U>

SafeInt<T,E>& operator>>= (U bits) throw()

>>=

template<typename U>

SafeInt<T,E>& operator>>= (SafeInt<U, E> bits) throw()

&

SafeInt<T,E> operator& (SafeInt<T,E> rhs) const throw()

&

template<typename U>

SafeInt<T,E> operator& (U rhs) const throw()

&=

SafeInt<T,E>& operator&= (SafeInt<T,E> rhs) throw()

&=

template<typename U>

SafeInt<T,E>& operator&= (U rhs) throw()

&=

template<typename U>

SafeInt<T,E>& operator&= (SafeInt<U, E> rhs) throw()

^

SafeInt<T,E> operator^ (SafeInt<T,E> rhs) const throw()

^

template<typename U>

SafeInt<T,E> operator^ (U rhs) const throw()

^=

SafeInt<T,E>& operator^= (SafeInt<T,E> rhs) throw()

^=

template<typename U>

SafeInt<T,E>& operator^= (U rhs) throw()

^=

template<typename U>

SafeInt<T,E>& operator^= (SafeInt<U, E> rhs) throw()

|

SafeInt<T,E> operator| (SafeInt<T,E> rhs) const throw()

|

template<typename U>

SafeInt<T,E> operator| (U rhs) const throw()

|=

SafeInt<T,E>& operator|= (SafeInt<T,E> rhs) throw()

|=

template<typename U>

SafeInt<T,E>& operator|= (U rhs) throw()

|=

template<typename U>

SafeInt<T,E>& operator|= (SafeInt<U, E> rhs) throw()

설명

SafeInt 정수 오버플로 수학 연산에 대 한 클래스를 보호 합니다.예를 들어, 두 개의 8 비트 정수를 추가 하는 것이 좋습니다: 한 200이 고 두 번째 값이 100 인.올바른 수학 작업 200 + 100 = 300.그러나 상위 비트는 8 비트 정수 제한 때문에 손실 됩니다 및 44 컴파일러를 반환 합니다 (300-28) 결과입니다.이 수학 방정식에 따라 달라 집니다 모든 작업 예기치 않은 동작이 발생 합니다.

SafeInt 클래스 산술 오버플로가 발생 하는지 또는 코드 0으로 나누려고 시도 하는지 여부를 확인 합니다.두 경우 모두 클래스 프로그램의 잠재적인 문제를 경고 하는 오류 처리기를 호출 합니다.

이 클래스 들은 대로 두 가지 유형의 정수를 비교할 수도 있습니다 SafeInt 개체입니다.일반적으로 비교를 수행할 때 동일한 형식으로 숫자를 먼저 변환 해야 합니다.종종 한 숫자를 다른 형식으로 캐스팅 되어 데이터가 손실 되지 않습니다 있는지 확인 해야 합니다.

이 항목의 연산자 테이블에서 지 원하는 수학 및 비교 연산자 목록에서 SafeInt 클래스입니다.반환할 가장 수치 연산자는 SafeInt 형식의 개체를 T.

비교 작업 사이 SafeInt 및 정수 계열 형식이 어느 한 방향으로 수행 될 수 있습니다.예를 들어, 둘 다 SafeInt<int>(x) < y 및 y > SafeInt<int>(x) 유효 하 고 동일한 결과 반환 합니다.

서로 다른 두 가지를 사용 하 여 다양 한 이항 연산자를 지원 하지 않는 SafeInt 형식입니다.한 예로 들 수 있는 & 연산자.SafeInt<T, E> & int지원 되지 않지만 SafeInt<T, E> & SafeInt<U, E> 수 없습니다.두 번째 예제에서는 반환할 매개 변수 유형을 컴파일러가 알지 못합니다.이 문제를 해결 하는 두 번째 매개 변수는 기본 형식으로 다시 캐스팅입니다.동일한 매개 변수를 사용 하 여이를 수행할 수 있습니다 SafeInt<T, E> & (U)SafeInt<U, E>.

[!참고]

비트 단위 작업의 경우 두 개의 다른 매개 변수는 동일한 크기가 같아야 합니다.컴파일러는 크기가 다를 경우에 throw 됩니다 있는 ASSERT (MFC) 예외입니다.이 작업의 결과가 정확 하지 않을 수 있습니다.이 문제를 해결 하려면 동일한 크기가 큰 매개 변수로 작은 매개 변수를 캐스팅 합니다.

이동 연산자에 대 한 서식 파일 종류에 대 한 보다 자세한 비트 시프트 ASSERT 예외를 throw 합니다.이 릴리스 모드에서 작동을 하지 않습니다.반환 형식은 원래 형식과 동일 하 게 이기 때문에 혼합 된 SafeInt 매개 변수는 시프트 연산자에 대 한 가능 합니다.연산자의 오른쪽에 수만의 이동할 비트 수를 나타냅니다.

SafeInt 개체와 논리 비교를 수행 하면 산술 엄격 하 게 비교가 수행 됩니다.예를 들어이 식을 고려해 야 합니다.

  • SafeInt<uint>((uint)~0) > -1

  • ((uint)~0) > -1

첫 번째 문은 확인 true, 하지만 두 번째 문은 확인 false.비트 부정 0 0xFFFFFFFF입니다.두 번째 문에서 기본 비교 연산자 0xFFFFFFFF 0xffffffff를 비교 하 고 동일 하 게 간주 합니다.비교 연산자는 SafeInt 클래스를 인식 하는 첫 번째 매개 변수는 서명 되지 않은 반면, 두 번째 매개 변수는 음수 임을.따라서, 비트 표현을 동일 하지만 SafeInt 논리 연산자를 인식 하는 부호 없는 정수-1 보다 큽니다.

사용할 때 주의 SafeInt 함께 클래스는 ?: 삼항 연산자.다음 코드 줄을 고려해 야 합니다.

Int x = flag ? SafeInt<unsigned int>(y) : -1;

컴파일러가이를 변환합니다.

Int x = flag ? SafeInt<unsigned int>(y) : SafeInt<unsigned int>(-1);

경우 flag 입니다 false, 컴파일러는 값-1 할당 하는 대신 예외를 throw 합니다. x.따라서이 문제를 방지 하려면 다음 줄 사용 하는 올바른 코드가입니다.

Int x = flag ? (int) SafeInt<unsigned int>(y) : -1;

T및 U 부울 형식이 나 문자 형식 정수 형식에 할당할 수 있습니다.형식 서명 되거나 서명 되지 않은 정수 및 64 비트에서 8 비트로 원하는 크기로 합니다.

[!참고]

하지만 SafeInt 클래스는 모든 종류의 정수 허용, 부호 없는 형식으로 보다 효율적으로 수행 합니다.

E되는 오류 처리 메커니즘은 SafeInt 사용 합니다.두 오류 처리 메커니즘에 SafeInt 라이브러리가 제공 됩니다.기본 정책 SafeIntErrorPolicy_SafeIntException, 어떤 throw는 SafeIntException 클래스 예외 오류가 발생 한 경우.다른 정책 SafeIntErrorPolicy_InvalidParameter, 어떤 중지 프로그램 오류가 발생 하는 경우.

오류 정책을 사용자 지정 하는 방법은 다음 두 가지가 있습니다.매개 변수를 설정 하는 것이 첫 번째 옵션 E 을 만들 때는 SafeInt.오류 처리 정책에 대해 하나만 변경 하려는 경우이 옵션을 사용 합니다. SafeInt.다른 옵션을 정의 하는 것 _SAFEINT_DEFAULT_ERROR_POLICY 포함 하 여 전에 사용자 지정된 오류 처리 클래스에는 SafeInt 라이브러리.기본 오류 처리의 모든 인스턴스에 대 한 정책 변경 하려는 경우이 옵션을 사용의 SafeInt 코드에서 클래스.

[!참고]

SafeInt 라이브러리에서 오류를 처리 하는 사용자 지정된 클래스 컨트롤 오류 처리기를 호출 하는 코드에 반환지 않습니다.오류 처리기가 호출 되 면 결과 SafeInt 작업 신뢰할 수 없습니다.

요구 사항

헤더: safeint.h

네임 스페이스: msl::utilities

참고 항목

참조

SafeIntException 클래스

기타 리소스

기타 지원 라이브러리 클래스

SafeInt 라이브러리