std::tuple and standard layout

前端 未结 4 2363
感情败类
感情败类 2021-02-19 05:10

If all of the members of std::tuple are of standard layout types, is that std::tuple itself standard layout? The presence of a user-defined copy-constr

4条回答
  •  情书的邮戳
    2021-02-19 05:35

    The "list" approach can be used to get standard layout tuple (the following example has some inaccuracies but demonstrates the idea):

    template 
    struct tuple;
    
    template 
    struct tuple {
        T value;
        tuple next;
    };
    
    template <>
    struct tuple<> {};
    
    namespace details {
        template 
        struct get_impl {
            template 
            constexpr static auto process(const tuple& t) {
                return get_impl::process(t.next);
            }
        };
    
        template <>
        struct get_impl<0> {
            template 
            constexpr static auto process(const tuple& t) {
                return t.value;
            }
        };
    }
    
    template 
    constexpr auto get(const tuple& t) {
        return details::get_impl::process(t);
    }
    
    template 
    constexpr auto make_tuple(Args&&... args) {
        return tuple{std::forward(args)...};
    }
    

提交回复
热议问题