C++ Tuple vs Struct

后端 未结 12 1781
耶瑟儿~
耶瑟儿~ 2020-12-07 10:51

Is there is any difference between using a std::tuple and a data-only struct?

typedef std::tuple foo_t;

s         


        
12条回答
  •  一整个雨季
    2020-12-07 11:51

    As far as the "generic function" go, Boost.Fusion deserves some love... and especially BOOST_FUSION_ADAPT_STRUCT.

    Ripping from the page: ABRACADBRA

    namespace demo
    {
        struct employee
        {
            std::string name;
            int age;
        };
    }
    
    // demo::employee is now a Fusion sequence
    BOOST_FUSION_ADAPT_STRUCT(
        demo::employee
        (std::string, name)
        (int, age))
    

    This means that all Fusion algorithms are now applicable to the struct demo::employee.


    EDIT: Regarding the performance difference or layout compatibility, tuple's layout is implementation defined so not compatible (and thus you should not cast between either representation) and in general I would expect no difference performance-wise (at least in Release) thanks to the inlining of get.

提交回复
热议问题