std :: numeric_limits를 호출하기 전에 단항 "+"의 목적은 무엇입니까? 회원?
cppreference의 설명서 에서이 예제를 보았습니다.std::numeric_limits
#include <limits>
#include <iostream>
int main()
{
std::cout << "type\tlowest()\tmin()\t\tmax()\n\n";
std::cout << "uchar\t"
<< +std::numeric_limits<unsigned char>::lowest() << '\t' << '\t'
<< +std::numeric_limits<unsigned char>::min() << '\t' << '\t'
<< +std::numeric_limits<unsigned char>::max() << '\n';
std::cout << "int\t"
<< std::numeric_limits<int>::lowest() << '\t'
<< std::numeric_limits<int>::min() << '\t'
<< std::numeric_limits<int>::max() << '\n';
std::cout << "float\t"
<< std::numeric_limits<float>::lowest() << '\t'
<< std::numeric_limits<float>::min() << '\t'
<< std::numeric_limits<float>::max() << '\n';
std::cout << "double\t"
<< std::numeric_limits<double>::lowest() << '\t'
<< std::numeric_limits<double>::min() << '\t'
<< std::numeric_limits<double>::max() << '\n';
}
"+"연산자를 이해할 수 없습니다
<< +std::numeric_limits<unsigned char>::lowest()
나는 그것을 테스트하고 그것을 "-"로 바꾸었고, 그것은 효과가 있었다. 이러한 "+"연산자의 사용법은 무엇입니까?
(부호 또는 부호없는) <<
전달 될 때 출력 연산자 char
는 문자 로 씁니다 .
이 함수는 유형의 값을 반환합니다 unsigned char
. 그리고 위에서 언급했듯이 그 값은 정수 값이 아닌 현재 인코딩에서 나타내는 문자를 인쇄합니다.
+
운전자 변환은 unsigned char
내지 An 이러한 함수에 의해 리턴 int
통해 정수 추진 . 이는 정수 값이 대신 인쇄됨을 의미합니다.
같은 표현 +std::numeric_limits<unsigned char>::lowest()
은 본질적으로와 같습니다 static_cast<int>(std::numeric_limits<unsigned char>::lowest())
.
+
이 설정하는 것입니다 unsigned char
에 int
. +
연산자 값 유지이지만 피연산자에 일체 촉진을 유도하는 효과가있다. operator <<
문자 유형이 주어지면 인쇄되는 일부 (반) 임의 문자 대신 숫자 값이 표시되도록해야합니다 .
이미 주어진 답변에 대한 참조를 추가하십시오. CPP 표준 작업 초안 N4713에서 :
8.5.2.1 단항 연산자
...
- 단항 + 연산자의 피연산자는 산술, 범위가없는 열거 또는 포인터 유형을 가져야하며 결과는 인수 값입니다. 적분 승격은 적분 또는 열거 피연산자에서 수행됩니다. 결과 유형은 승격 된 피연산자의 유형입니다.
그리고 char
, short
, int
, 및 long
통합 유형입니다.
+
결과가 없으면 다를 것입니다. 다음 스 니펫 출력 a 97
대신a a
char ch = 'a';
std::cout << ch << ' ' << +ch << '\n';
그 이유는 다른 과부하가 다른 유형의 데이터를 인쇄 하기 때문 입니다. basic_ostream& operator<<( char value );
과부하 가 없으며 std::basic_ostream
페이지 끝에 설명되어 있습니다.
문자 및 문자열 인수 (예 :
char
또는const char*
)는 멤버 가 아닌 오버로드 에 의해 처리됩니다operator<<
. 출력 멤버 함수 호출 구문을 이용하여 문자를 시도 (예std::cout.operator<<('c');
) 과부하의 온 (2-4) 및 출력 수치를 호출한다 . 멤버 함수 호출 구문을 사용하여 문자열을 출력하려고하면 오버로드 (7)를 호출하고 대신 포인터 값을 인쇄합니다.
변수 를 전달할 때 호출 되는 비 멤버 오버로드char
는
template< class CharT, class Traits> basic_ostream<CharT,Traits>& operator<<(
basic_ostream<CharT,Traits>& os, char ch );
코드 포인트에서 문자를 인쇄합니다 ch
So basically if you pass char
, signed char
or unsigned char
directly to the stream it'll print the character out. If you try removing the +
on the above lines you'll see that it prints some "strange" or non-visible characters which is not what one would expect
If you want their numerical values instead you must call the overload for short
, int
, long
or long long
. The easiest way to do this is promoting from char
to int
with unary plus +
. That's one of the rare useful applications of the unary plus operator. An explicit cast to int
will also work
There are many people who faced that problem on SO like
'development' 카테고리의 다른 글
젠킨스에서 파이썬 단위 테스트? (0) | 2020.07.04 |
---|---|
클래스 패스 자원의 java.nio.file.Path (0) | 2020.07.04 |
문자열을 어떻게 연결합니까? (0) | 2020.07.04 |
knockout.js를 ASP.NET MVC ViewModels와 함께 사용하는 방법? (0) | 2020.07.04 |
PostgreSQL이 인덱스 열에서 순차적 스캔을 수행하는 이유는 무엇입니까? (0) | 2020.07.04 |