2015年5月21日木曜日

size_t(C++)

size_t

長さ、大きさ、サイズ(メモリ量)を表現する型として用いるもので、sizeof演算子が返す符号なしの整数型(unsigned int)である。

//
// sizeof.cpp
// CplusplusPractice
//
// Created by masai on 2015/05/21.
// Copyright (c) 2015年 masai. All rights reserved.
//
#include <iostream>
using namespace std;
template<class ... T>
struct S{
static const size_t count = sizeof...(T);
};
int main(){
// int型で確保されるメモリサイズを出力
cout << sizeof 1 << endl;
int a = 2;
// sizeofに与えられた式は評価されないため、後に出力するaは2のまま
cout << sizeof ++a << endl;
cout << a << endl;
// 式でなく、型を与えても良い
cout << sizeof(int) << endl;
cout << sizeof(float) << endl;
cout << sizeof(double) << endl;
// 可変長引数の個数を取得する場合
S<int, char, double> t;
cout << t.count << endl;
}
view raw sizeof.cpp hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿