tupleのサイズ

ちょっと気になる所があったので,

  • std::tuple (C++0x)
  • boost::tuple
  • boost::fusion::vector

について大きさを調べてみました.fusion::vectorはタプルというよりも,タプル+αという感じですが….

コンパイラによっても差が出そうだなーと思いつつも,VCがすぐに動く環境がないのでg++ 4.5.1のみです.

#include <iostream>
#include <tuple>
#include <boost/tuple/tuple.hpp>
#include <boost/fusion/include/vector.hpp>

template <class T>
void size() {
    std::cout << sizeof(T) << std::endl;
}

struct T {};
struct U {};

int main() {
    size< std::tuple<T, U> >();
    size< boost::tuple<T, U> >();
    size< boost::fusion::vector<T, U> >();

    std::cout << std::endl;

    size< std::tuple<int, T, U> >();
    size< boost::tuple<int, T, U> >();
    size< boost::fusion::vector<int, T, U> >();

    return 0;
}

このコードをコンパイルして実行すると,

1
2
3

4
8
12

という出力が得られました.

std::tupleはEBOがかかっている感じです.boost::tupleはstructに要素を並べたサイズに,fusion::vectorはそれに+1要素という印象を受けます.