Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

C++의 유형 추론(auto 및 decltype)

<시간/>

이 자습서에서는 C++(auto 및 decltype)의 유형 간섭을 이해하는 프로그램에 대해 설명합니다.

auto 키워드의 경우 변수의 유형은 초기화 프로그램의 유형에서 정의됩니다. 또한 decltype을 사용하면 호출된 요소에서 변수 유형을 추출할 수 있습니다.

자동 유형

예시

#include <bits/stdc++.h>
using namespace std;
int main(){
   auto x = 4;
   auto y = 3.37;
   auto ptr = &x;
   cout << typeid(x).name() << endl
      << typeid(y).name() << endl
      << typeid(ptr).name() << endl;
   return 0;
}

출력

i
d
Pi

decl 유형

예시

#include <bits/stdc++.h>
using namespace std;
int fun1() { return 10; }
char fun2() { return 'g'; }
int main(){
   decltype(fun1()) x;
   decltype(fun2()) y;
   cout << typeid(x).name() << endl;
   cout << typeid(y).name() << endl;
   return 0;
}

출력

i
c