autoとdecltypeの2種類の推定
5547 ワード
autoとdecltypeの違いは主に3つの面があります. autoタイプ計算後推定、decltypeは計算しない. autoは最上位constを無視し、下位constを保持します.decltypeはすべて保持されます. はautoとは異なり、decltypeの結果タイプは式形式と密接に関連している.かっこをつけるのとつけないのとでは違います.
Autoの推定ポリシー
推定の真髄は右値にあり,右値の形式に基づいて,簡単な単純推定と複合推定である.ポインタを参照するときは、非表示のconstの入力に注意し、const autoで宣言する必要があります.
例えば次のコード
完全なテストコード
#include
#include
using namespace std;
int main()
{
int a = 3;
auto c1 = a;
decltype(a) c2 = a;
decltype((a)) c3 = a;
const int d = 5;
auto f1 = d;
decltype(d) f2 = d;
cout << typeid(c1).name() << endl;
cout << typeid(c2).name() << endl;
cout << typeid(c3).name() << endl;
cout << typeid(f1).name() << endl;
cout << typeid(f2).name() << endl;
c1 ++;
c2 ++;
c3 ++;
f1 ++;
//f2 ++;
cout << a << " " << c1 << ' ' << c2 << " " << c3 << ' ' << f1 << ' ' << f2 << endl;
}
Autoの推定ポリシー
推定の真髄は右値にあり,右値の形式に基づいて,簡単な単純推定と複合推定である.ポインタを参照するときは、非表示のconstの入力に注意し、const autoで宣言する必要があります.
例えば次のコード
int i=100;
cosnt auto ci=23;//right
auto ci=23;//wrong
auto &test_i=i;// ,test_i int , i int
const auto &p=&i;//right, , , i &i , , const 。
auto &q=&i;//wrong,
auto &x=p;//right
完全なテストコード
#include
#include
using namespace std;
int main()
{
int i=1,&ri=i;
const int ci=21,&rci=ci;
// int
auto a=1;
auto b=i;
auto c=ri;
auto d=ci;
auto e=rci;
auto f=&i;//
auto g=&ci;// , const
// ( )
//auto &ra=1;//
auto &rb=i;rb++;
auto &rc=ri;rc++;
auto &rd=ci;// , const int
auto &re=rci;// , const int
//auto &k=21;
const auto &k=21;
const auto ck=ci;
const auto cki=i;
//
auto *p=&ci;
auto q=&ci;
cout<<typeid(p).name()<<' '<cout<<typeid(q).name()<<' '<return 0;
}