C++タイプ処理

4783 ワード

ポインタへの参照
参照自体はオブジェクトではないので、参照を指すポインタは定義できませんが、ポインタはオブジェクトなので、ポインタの参照があります.pがポインタの参照である理由を右から左に理解することができ、pの左が参照記号であるため、pは参照であり、残りの部分はpがintポインタを参照していることを示す.
int main()
{
    int value1 = 10;
    int *value2 = &value1;
    int *&p = value2;
    *p = 100;
    std::cout << value1 << std::endl;//100
    return 0;
}

最上位constと最下位const
最上位constはポインタ自体が定数であり、最下位constはオブジェクトが定数であることを示します.
int main(void)
{
    int value1 = 10;
    const int *value2 = &value1;//  const int      const
    int* const value3 = &value1;//  int        const

    *value2 = 100;//  ,          ,const int
    value2 = nullptr;//  ,        

    *value3 = 100;//  ,      int
    value3 = nullptr;//  ,       
    return 0;
}

タイプ別名
タイプ別名typedefとusingを定義するには、2つの方法があります.
typedef int xyint;//xyint int     
typedef int *pxyint;//pxyint int*     
using xydouble = double;//xydouble double     
using pxydouble = double*;//pxydouble double*     

int main(void)
{
    xyint value1 = 0;
    xydouble value2 = 3.14;
    pxyint value3 = nullptr;
    pxydouble value4 = nullptr;
    return 0;
}



これらの違いはtypedefがテンプレートクラスにタイプ別名を設定できないことです.
template 
typedef std::vector vType;//  


template 
using vType = std::vector;//  

タイプ別名、const、ポインタ
タイプ別名がポインタタイプを指す場合、constで修飾すると予想外の結果が発生します.たとえば、次のconst pxyintは、実際にはintを指す定数ポインタです.タイプ別名が直接置き換えられていないため、pxyintを全体と見なすべきです.
typedef int *pxyint;
using pxydouble = double*;

int main(void)
{
    double value1 = 3.14;
    int value2 = 10;

    const pxyint value3 = &value2;//  int        const
    const pxydouble value4 = &value1;//  double        const
    *value3 = 100;//  
     value3 = nullptr;//  

    *value4 = 100;//  
    value4  = nullptr;//  
    return 0;
}

pxyintが#defineで定義されている場合、const pxyintは最下位のconstです.
#define pxyint int * 

int main(void)
{
    int value1 = 10;
    const pxyint value2 = &value1;//  const int      const

    *value2 = 100;//  
     value2 = nullptr;//  
    return 0;
}

auto
1つの式のタイプを全く知らない場合や、難しい場合があります.C++11はautoタイプ説明子を導入し、コンパイラに式のタイプを自動的に分析させるため、auto定義の変数には初期値が必要です.
int main(void)
{
    int value1 = 10;
    auto value2 = value1 + 100;//value2    int
    return 0;
}

タイプ別名と同様に、autoタイプがポインタタイプの場合、const修飾を使用すると、下位constではなく最上位constが得られます.
int main(void)
{
    int value1 = 0,*value2=&value1;
    const auto value3 = value2;//  const
    *value3 = 100;//  
    value3 = nullptr;//  
    return 0;
}

autoを使用して複数の変数を定義する場合は、タイプが同じである必要があります.
int main(void)
{
    auto value1 = 0, *value2 = &value1;//  ,value1   ,value2     
    auto value3 = 0, value4 = 3.14;//  ,value3 value4     
    return 0;
}

参照タイプが初期値である場合、autoが推定したタイプは参照オブジェクトのタイプであり、推定したいものが参照である場合は明確に指摘する必要がある.
int main(void)
{
    int value1 = 0, &value2 = value1;
    auto value3 = value2;//value3   int
    auto &value4 = value1;//value4     
    return 0;
}


autoは最上位constを無視し、最下位constを保持します.最上位constを推定するには、明確に指摘する必要があります.
int main(void)
{
    const int value1 = 0;
    auto value2 = value1;//value2 int  
    const auto value3 = value1;//value3 const int  
    return 0;
}

autoタイプの参照を設定すると、初期値の最上位constが保持されます.
int main(void)
{
    const int value1 = 0;
    auto &value2 = value1;//value2 const int
    return 0;
}

decltype
式を使用して推定されたタイプを使用したい場合がありますが、この式を呼び出したくない場合があります.C++11はdecltypeタイプインジケータを導入し、式のデータ型を返す役割を果たしますが、この式は呼び出されません.
int main(void)
{
    int value1 = 0;
    decltype(value1) value2 = 100;//value2   int
    return 0;
}

autoとは異なり、decltypeは式の最上位constと参照を無視しません.
int main(void)
{
    const int value1 = 0;
    const int &value2 = value1;
    decltype(value1) value3 = 100;//value3   const int
    decltype(value2) value4 = value1;//value4 const int   
    return 0;
}

decltype式の内容がポインタの解参照操作である場合、結果は参照になります.
int main(void)
{
    int *value1 = nullptr;
    decltype(value1) value2;//value2     
    decltype(*value1) value3;//value3 int   
    return 0;
}

decltype式が二重カッコの場合、結果は常に参照され、初期化する必要があります.
int main(void)
{
    int value1 = 0;
    decltype((value1)) value2;
    return 0;
}

また、標準タイプ変換テンプレートを使用して、1つの参照から非参照タイプを取得することもできます.
int main()
{
    int value1 = 0;
    int& value2 = value1;
    remove_reference::type value3=100;
    cout <