Pretty-print std :: tuple

时间:2011-06-05 20:43:20

标签: c++ c++11 tuples variadic-templates

这是我之前关于pretty-printing STL containers的问题的后续行动,为此我们设法制定了一个非常优雅且完全通用的解决方案。


在下一步中,我想使用可变参数模板为std::tuple<Args...>包含漂亮打印(因此这是严格的C ++ 11)。对于std::pair<S,T>,我只是说

std::ostream & operator<<(std::ostream & o, const std::pair<S,T> & p)
{
  return o << "(" << p.first << ", " << p.second << ")";
}

打印元组的类似结构是什么?

我已经尝试了各种模板参数堆栈解包,传递索引并使用SFINAE来发现我何时处于最后一个元素,但没有成功。我不会用破碎的代码给你带来负担;问题描述有希望直截了当。基本上,我想要以下行为:

auto a = std::make_tuple(5, "Hello", -0.1);
std::cout << a << std::endl; // prints: (5, "Hello", -0.1)

与上一个问题包含相同的一般性(char / wchar_t,pair delimiters)的奖励积分!

13 个答案:

答案 0 :(得分:75)

是的,indices

namespace aux{
template<std::size_t...> struct seq{};

template<std::size_t N, std::size_t... Is>
struct gen_seq : gen_seq<N-1, N-1, Is...>{};

template<std::size_t... Is>
struct gen_seq<0, Is...> : seq<Is...>{};

template<class Ch, class Tr, class Tuple, std::size_t... Is>
void print_tuple(std::basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is...>){
  using swallow = int[];
  (void)swallow{0, (void(os << (Is == 0? "" : ", ") << std::get<Is>(t)), 0)...};
}
} // aux::

template<class Ch, class Tr, class... Args>
auto operator<<(std::basic_ostream<Ch, Tr>& os, std::tuple<Args...> const& t)
    -> std::basic_ostream<Ch, Tr>&
{
  os << "(";
  aux::print_tuple(os, t, aux::gen_seq<sizeof...(Args)>());
  return os << ")";
}

Live example on Ideone.


对于分隔符,只需添加这些部分特化:

// Delimiters for tuple
template<class... Args>
struct delimiters<std::tuple<Args...>, char> {
  static const delimiters_values<char> values;
};

template<class... Args>
const delimiters_values<char> delimiters<std::tuple<Args...>, char>::values = { "(", ", ", ")" };

template<class... Args>
struct delimiters<std::tuple<Args...>, wchar_t> {
  static const delimiters_values<wchar_t> values;
};

template<class... Args>
const delimiters_values<wchar_t> delimiters<std::tuple<Args...>, wchar_t>::values = { L"(", L", ", L")" };

并相应地更改operator<<print_tuple

template<class Ch, class Tr, class... Args>
auto operator<<(std::basic_ostream<Ch, Tr>& os, std::tuple<Args...> const& t)
    -> std::basic_ostream<Ch, Tr>&
{
  typedef std::tuple<Args...> tuple_t;
  if(delimiters<tuple_t, Ch>::values.prefix != 0)
    os << delimiters<tuple_t,char>::values.prefix;

  print_tuple(os, t, aux::gen_seq<sizeof...(Args)>());

  if(delimiters<tuple_t, Ch>::values.postfix != 0)
    os << delimiters<tuple_t,char>::values.postfix;

  return os;
}

template<class Ch, class Tr, class Tuple, std::size_t... Is>
void print_tuple(std::basic_ostream<Ch, Tr>& os, Tuple const& t, seq<Is...>){
  using swallow = int[];
  char const* delim = delimiters<Tuple, Ch>::values.delimiter;
  if(!delim) delim = "";
  (void)swallow{0, (void(os << (Is == 0? "" : delim) << std::get<Is>(t)), 0)...};
}

答案 1 :(得分:18)

我在C ++ 11(gcc 4.7)中运行良好。我确信有些陷阱我没有考虑过,但我认为代码很容易阅读而且并不复杂。唯一可能奇怪的是“guard”struct tuple_printer,它确保我们在到达最后一个元素时终止。另一个奇怪的事情可能是sizeof ...(类型)返回Types类型pack中的类型数。它用于确定最后一个元素的索引(大小...(类型) - 1)。

template<typename Type, unsigned N, unsigned Last>
struct tuple_printer {

    static void print(std::ostream& out, const Type& value) {
        out << std::get<N>(value) << ", ";
        tuple_printer<Type, N + 1, Last>::print(out, value);
    }
};

template<typename Type, unsigned N>
struct tuple_printer<Type, N, N> {

    static void print(std::ostream& out, const Type& value) {
        out << std::get<N>(value);
    }

};

template<typename... Types>
std::ostream& operator<<(std::ostream& out, const std::tuple<Types...>& value) {
    out << "(";
    tuple_printer<std::tuple<Types...>, 0, sizeof...(Types) - 1>::print(out, value);
    out << ")";
    return out;
}

答案 2 :(得分:14)

我很惊讶cppreference上的实施尚未在此发布,所以我会为后代做这件事。它隐藏在std::tuple_cat的文档中,因此不容易找到。它像其他一些解决方案一样使用了一个保护结构,但我认为它们最终更简单,更容易理解。

#include <iostream>
#include <tuple>
#include <string>

// helper function to print a tuple of any size
template<class Tuple, std::size_t N>
struct TuplePrinter {
    static void print(const Tuple& t) 
    {
        TuplePrinter<Tuple, N-1>::print(t);
        std::cout << ", " << std::get<N-1>(t);
    }
};

template<class Tuple>
struct TuplePrinter<Tuple, 1> {
    static void print(const Tuple& t) 
    {
        std::cout << std::get<0>(t);
    }
};

template<class... Args>
void print(const std::tuple<Args...>& t) 
{
    std::cout << "(";
    TuplePrinter<decltype(t), sizeof...(Args)>::print(t);
    std::cout << ")\n";
}
// end helper function

测试:

int main()
{
    std::tuple<int, std::string, float> t1(10, "Test", 3.14);
    int n = 7;
    auto t2 = std::tuple_cat(t1, std::make_pair("Foo", "bar"), t1, std::tie(n));
    n = 10;
    print(t2);
}

输出:

  

(10,Test,3.14,Foo,bar,10,Test,3.14,10)

Live Demo

答案 3 :(得分:13)

在C ++ 17中,我们可以通过利用Fold expressions来减少代码来实现这一点,特别是一元左折:

template<class TupType, size_t... I>
void print(const TupType& _tup, std::index_sequence<I...>)
{
    std::cout << "(";
    (..., (std::cout << (I == 0? "" : ", ") << std::get<I>(_tup)));
    std::cout << ")\n";
}

template<class... T>
void print (const std::tuple<T...>& _tup)
{
    print(_tup, std::make_index_sequence<sizeof...(T)>());
}

Live Demo输出:

  

(5,你好,-0.1)

给定的

auto a = std::make_tuple(5, "Hello", -0.1);
print(a);

说明

我们的一元左折是

的形式
... op pack

其中op在我们的场景中是逗号运算符,pack是包含我们在未展开的上下文中的元组的表达式,如:

(..., (std::cout << std::get<I>(myTuple))

所以如果我有这样的元组:

auto myTuple = std::make_tuple(5, "Hello", -0.1);

一个std::integer_sequence,其值由非类型模板指定(参见上面的代码)

size_t... I

然后是表达式

(..., (std::cout << std::get<I>(myTuple))

扩展为

((std::cout << std::get<0>(myTuple)), (std::cout << std::get<1>(myTuple))), (std::cout << std::get<2>(myTuple));

将打印

  

5Hello-0.1

这是严重的,所以我们需要做一些更多的技巧来添加一个逗号分隔符,以便首先打印,除非它是第一个元素。

为实现这一目标,如果当前索引pack不是第一个,那么我们会修改fold表达式的" ,"部分以打印I,因此(I == 0? "" : ", ")部分< SUP> * :

(..., (std::cout << (I == 0? "" : ", ") << std::get<I>(_tup)));

现在我们得到

  

5,您好,-0.1

哪个看起来更好(注意:我想要类似的输出为this answer

*注意:您可以通过各种方式进行逗号分隔,而不是最终结果。我最初通过对std::tuple_size<TupType>::value - 1进行测试,有条件地在之后添加逗号而不是之前的,但这太长了,所以我针对sizeof...(I) - 1进行了测试,但是最后我复制了Xeo,最后得到了我所拥有的。

答案 4 :(得分:3)

基于The C++ Programming Language By Bjarne Stroustrup, page 817上的示例:

#include <tuple>
#include <iostream>
#include <string>
#include <type_traits>
template<size_t N>
struct print_tuple{
    template<typename... T>static typename std::enable_if<(N<sizeof...(T))>::type
    print(std::ostream& os, const std::tuple<T...>& t) {
        char quote = (std::is_convertible<decltype(std::get<N>(t)), std::string>::value) ? '"' : 0;
        os << ", " << quote << std::get<N>(t) << quote;
        print_tuple<N+1>::print(os,t);
        }
    template<typename... T>static typename std::enable_if<!(N<sizeof...(T))>::type
    print(std::ostream&, const std::tuple<T...>&) {
        }
    };
std::ostream& operator<< (std::ostream& os, const std::tuple<>&) {
    return os << "()";
    }
template<typename T0, typename ...T> std::ostream&
operator<<(std::ostream& os, const std::tuple<T0, T...>& t){
    char quote = (std::is_convertible<T0, std::string>::value) ? '"' : 0;
    os << '(' << quote << std::get<0>(t) << quote;
    print_tuple<1>::print(os,t);
    return os << ')';
    }

int main(){
    std::tuple<> a;
    auto b = std::make_tuple("One meatball");
    std::tuple<int,double,std::string> c(1,1.2,"Tail!");
    std::cout << a << std::endl;
    std::cout << b << std::endl;
    std::cout << c << std::endl;
    }

输出:

()
("One meatball")
(1, 1.2, "Tail!")

答案 5 :(得分:2)

基于AndyG代码,适用于C ++ 17

#include <iostream>
#include <tuple>

template<class TupType, size_t... I>
std::ostream& tuple_print(std::ostream& os,
                          const TupType& _tup, std::index_sequence<I...>)
{
    os << "(";
    (..., (os << (I == 0 ? "" : ", ") << std::get<I>(_tup)));
    os << ")";
    return os;
}

template<class... T>
std::ostream& operator<< (std::ostream& os, const std::tuple<T...>& _tup)
{
    return tuple_print(os, _tup, std::make_index_sequence<sizeof...(T)>());
}

int main()
{
    std::cout << "deep tuple: " << std::make_tuple("Hello",
                  0.1, std::make_tuple(1,2,3,"four",5.5), 'Z')
              << std::endl;
    return 0;
}

输出:

deep tuple: (Hello, 0.1, (1, 2, 3, four, 5.5), Z)

答案 6 :(得分:2)

利用std::apply(C ++ 17),我们可以删除std::index_sequence并定义一个函数:

#include <tuple>
#include <iostream>

template<class Ch, class Tr, class... Args>
auto& operator<<(std::basic_ostream<Ch, Tr>& os, std::tuple<Args...> const& t) {
  std::apply([&os](auto&&... args) {((os << args << " "), ...);}, t);
  return os;
}

或者,在stringstream的帮助下进行点缀:

#include <tuple>
#include <iostream>
#include <sstream>

template<class Ch, class Tr, class... Args>
auto& operator<<(std::basic_ostream<Ch, Tr>& os, std::tuple<Args...> const& t) {
  std::basic_stringstream<Ch, Tr> ss;
  ss << "[ ";
  std::apply([&ss](auto&&... args) {((ss << args << ", "), ...);}, t);
  ss.seekp(-2, ss.cur);
  ss << " ]";
  return os << ss.str();
}

答案 7 :(得分:1)

答案 8 :(得分:1)

另一个,类似于@Tony Olsson的,包括@Kerrek SB建议的空元组专业化。

#include <tuple>
#include <iostream>

template<class Ch, class Tr, size_t I, typename... TS>
struct tuple_printer
{
    static void print(std::basic_ostream<Ch,Tr> & out, const std::tuple<TS...> & t)
    {
        tuple_printer<Ch, Tr, I-1, TS...>::print(out, t);
        if (I < sizeof...(TS))
            out << ",";
        out << std::get<I>(t);
    }
};
template<class Ch, class Tr, typename... TS>
struct tuple_printer<Ch, Tr, 0, TS...>
{
    static void print(std::basic_ostream<Ch,Tr> & out, const std::tuple<TS...> & t)
    {
        out << std::get<0>(t);
    }
};
template<class Ch, class Tr, typename... TS>
struct tuple_printer<Ch, Tr, -1, TS...>
{
    static void print(std::basic_ostream<Ch,Tr> & out, const std::tuple<TS...> & t)
    {}
};
template<class Ch, class Tr, typename... TS>
std::ostream & operator<<(std::basic_ostream<Ch,Tr> & out, const std::tuple<TS...> & t)
{
    out << "(";
    tuple_printer<Ch, Tr, sizeof...(TS) - 1, TS...>::print(out, t);
    return out << ")";
}

答案 9 :(得分:0)

我喜欢DarioP的答案,但是stringstream使用堆。这可以避免:

template <class... Args>
std::ostream& operator<<(std::ostream& os, std::tuple<Args...> const& t) {
  os << "(";
  bool first = true;
  std::apply([&os, &first](auto&&... args) {
    auto print = [&] (auto&& val) {
      if (!first)
        os << ",";
      (os << " " << val);
      first = false;
    };
    (print(args), ...);
  }, t);
  os << " )";
  return os;
}

答案 10 :(得分:0)

我不喜欢以前使用折叠表达式的答案的一件事是,它们使用索引序列或标志来跟踪第一个元素,这消除了漂亮的干净折叠表达式的许多好处。

这里是一个不需要索引但可以达到类似结果的示例。 (不像其他一些复杂,但是可以添加更多。)

技术是使用折痕已经给您的东西:一种元素的特殊情况。也就是说,一个元素的折叠仅扩展到elem[0],然后2个元素是elem[0] + elem[1],其中+是一些运算。我们想要的是让一个元素仅将该元素写入流,而对于更多元素,执行相同的操作,但是将每个元素都附加写入“,”。因此,将其映射到c ++折叠,我们希望每个元素都是将某些对象写入流的动作。我们希望我们的+操作是用“,”写点缀两个写。因此,首先将元组序列转换为一系列写操作,我称之为CommaJoiner,然后为该操作添加一个operator+以我们想要的方式加入两个操作,并在其中添加一个“,”之间:

#include <tuple>
#include <iostream>

template <typename T>
struct CommaJoiner
{
    T thunk;
    explicit CommaJoiner(const T& t) : thunk(t) {}

    template <typename S>
    auto operator+(CommaJoiner<S> const& b) const
    {
        auto joinedThunk = [a=this->thunk, b=b.thunk] (std::ostream& os) {
            a(os);
            os << ", ";
            b(os);
        };
        return CommaJoiner<decltype(joinedThunk)>{joinedThunk};
    }

    void operator()(std::ostream& os) const
    {
        thunk(os);
    }

};

template <typename ...Ts>
std::ostream& operator<<(std::ostream& os, std::tuple<Ts...> tup)
{
    std::apply([&](auto ...ts) {
        return (... + CommaJoiner{[=](auto&os) {os << ts;}});}, tup)(os);

    return os;
}

int main() {
    auto tup = std::make_tuple(1, 2.0, "Hello");
    std::cout << tup << std::endl;
}

粗略地看一下Godbolt,这表明编译效果也很好,所有的thunk调用都变得平坦了。

这将需要第二次重载来处理一个空的元组。

答案 11 :(得分:0)

这是我最近为打印元组而编写的一些代码。

#include <iostream>
#include <tuple>

using namespace std;

template<typename... Ts>
ostream& operator<<(ostream& output, const tuple<Ts...> t) {
    output << '(';
    apply([&](auto&&... args) {
        ((cout << args << ", "), ...);
    }, t);
    output << "\b\b";
    output << ')';
    return output;
}

使用示例案例:

auto a = std::make_tuple(5, "Hello", -0.1); 
cout << a << '\n'; // (5, Hello, -0.1)

答案 12 :(得分:0)

我看到在 C++17 中使用 std::index_sequence 的答案,但是,这不是我个人会走的路。我宁愿去递归和constexpr if

#include <tuple>
#include <iostream>

template<std::size_t I, class... Ts>
void doPrintTuple(const std::tuple<Ts...>& tuples) {
    if constexpr (I == sizeof...(Ts)) {
        std::cout << ')';
    }
    else {
        std::cout << std::get<I>(tuples);
        if constexpr (I + 1 != sizeof...(Ts)) {
            std::cout << ", ";
        }
        doPrintTuple<I + 1>(tuples);
    }
}

template<class... Ts>
void printTuple(const std::tuple<Ts...>& tuples) {
    std::cout << '(';
    doPrintTuple<0>(tuples);
}

int main() {
    auto tup = std::make_tuple(1, "hello", 4.5);
    printTuple(tup);
}

输出:

(1, hello, 4.5)