C++11 向元组追加值

C++11 向元组追加值,c++11,C++11,我有一个元组: std::tuple<int, std::string, bool> foo = { 10, "Hello, world!", false }; 我应该如何编写一个通用函数,将一个值(甚至多个值,如果可能)附加到一个新的元组中 std::tuple<int, std::string, bool, MyClass> fooBar = tuple_append(foo, bar);

我有一个元组:

std::tuple<int, std::string, bool> foo = { 10, "Hello, world!", false };
我应该如何编写一个通用函数,将一个值(甚至多个值,如果可能)附加到一个新的元组中

std::tuple<int, std::string, bool, MyClass> fooBar = tuple_append(foo, bar);
                                                     ^^^^^^^^^^^^
                                            // I need this magical function!
std::tuple fooBar=tuple\u append(foo,bar);
^^^^^^^^^^^^
//我需要这个神奇的功能!
使用(如已注释):

#包括
#包括
#包括
int main()
{
std::tuple foo{10,“你好,世界!”,false};
auto foo_ext=std::tuple_cat(foo,std::make_tuple('a'));

std::cout对于附加单个元素,这将起作用:

template <typename NewElem, typename... TupleElem>
std::tuple<TupleElem..., NewElem> tuple_append(const std::tuple<TupleElem...> &tup, const NewElem &el) {
    return std::tuple_cat(tup, std::make_tuple(el));
}
模板
std::tuple tuple\u append(const std::tuple&tup,const NewElem&el){
返回std::tuple_cat(tup,std::make_tuple(el));
}

您不能简单地使用吗?
#include <iostream>
#include <string>
#include <tuple>

int main()
{
    std::tuple<int, std::string, bool> foo { 10, "Hello, world!", false };

    auto foo_ext = std::tuple_cat(foo, std::make_tuple('a'));

    std::cout << std::get<0>(foo_ext) << "\n"
              << std::get<1>(foo_ext) << "\n"
              << std::get<2>(foo_ext) << "\n"
              << std::get<3>(foo_ext) << "\n";
}
10 Hello, world! 0 a
template <typename NewElem, typename... TupleElem>
std::tuple<TupleElem..., NewElem> tuple_append(const std::tuple<TupleElem...> &tup, const NewElem &el) {
    return std::tuple_cat(tup, std::make_tuple(el));
}