tom*_*tom 5 c++ initializer-list auto c++11 type-deduction
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int i{100};
float f{3.14};
double d{3.14159};
cout<<"size of int is: " <<sizeof(i)<<endl;
cout<<"size of float is: " <<sizeof(f)<<endl;
cout<<"size of double is: "<<sizeof(d)<<endl<<endl;;
auto x = sin(3.14159);
cout<<"size of auto that is double is: "<<sizeof(x)<<endl<<endl;
auto y{sin(3.14159)};
cout<<"size of auto that is double is: "<<sizeof(y)<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
int的大小是:4
浮动的大小是:4
双倍的大小是:8
auto的大小是双倍的:8
auto的大小是双倍的:16
为什么是sizeof(y)16?
使用gcc 4.8.4的"typeid"如下:
#include <iostream>
#include <math.h>
#include <typeinfo>
using namespace std;
int main() {
int i{100};
float f{3.14};
double d{3.14159};
cout<<"size of int is: " <<sizeof(i)<<endl;
cout<<"size of float is: " <<sizeof(f)<<endl;
cout<<"size of double is: "<<sizeof(d)<<endl<<endl;;
auto x = sin(3.14159);
cout<<"size of auto that is double is: "<<sizeof(x)<<endl<<endl;
auto y{sin(3.14159)};
cout<<"size of auto that is double is: "<<sizeof(y)<<endl;
cout << typeid(y).name() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
$ ./main
size of int is: 4
size of float is: 4
size of double is: 8
size of auto that is double is: 8
size of auto that is double is: 16
St16initializer_listIdE
Run Code Online (Sandbox Code Playgroud)
我认为"auto y"实际上并没有被赋予双重权限,而是其中之一:http: //en.cppreference.com/w/cpp/utility/initializer_list
它说"std :: initializer_list类型的对象是一个轻量级代理对象,它提供对const T类型对象数组的访问".
所以很可能额外的空间用于存储指针和向量的大小,或类似的东西.
| 归档时间: |
|
| 查看次数: |
856 次 |
| 最近记录: |