Modern C++ std::tuple的size

不知道大家读过《Modern C++ std::unique_ptr的实现原理》没有?
里面提到了std::tuple的大小是4,而不是4+1或者4+4,是不是很奇怪,本文不会揭晓答案,只是会扩展测试各种情况。

#include
#include 
using namespace std;

struct Empty{
    constexpr Empty() noexcept = default;
};

int main(){
    std::cout<<"sizeof(Empty):"<<sizeof(Empty)<<std::endl;

    std::cout<<"sizeof(std::tuple):"<<sizeof(std::tuple<int,Empty>)<<std::endl;
    std::cout<<"sizeof(std::tuple):"<<sizeof(std::tuple<int,Empty,Empty,int>)<<std::endl;
    std::cout<<"sizeof(std::tuple):"<<sizeof(std::tuple<int,Empty,Empty,Empty,Empty,int>)<<std::endl;
    std::cout<<"sizeof(std::tuple):"<<sizeof(std::tuple<int,Empty,Empty,Empty,Empty,Empty>)<<std::endl;
    std::cout<<"sizeof(Empty[100]):"<<sizeof(Empty[100])<<std::endl;
}

有兴趣的同学试着运行一下?
Modern C++ std::tuple的size_第1张图片

答案是:

sizeof(Empty):1
sizeof(std::tuple):4
sizeof(std::tuple):12
sizeof(std::tuple):12
sizeof(std::tuple):8
sizeof(Empty[100]):100

你可能感兴趣的:(modern,C++,c++,开发语言,modern,c++,tuple)