Gcc C+中元组的递归打印+;
关于如何打印Gcc C+中元组的递归打印+;,gcc,c++11,clang,tuples,variadic-templates,Gcc,C++11,Clang,Tuples,Variadic Templates,关于如何打印元组,有几种建议。下面的代码片段从重载运算符中复制了Kenny的答案您可以在此处了解clang不编译它的原因:。看来,克朗的行为符合标准 为了解决这个问题,您可以定义操作符,下面是一个使用integer_序列的示例:只想指出链接的答案并不能解决编译问题-它仍然失败。这只是打印元组的另一种技术。糟糕,问题当然是名称空间:(我怎么会忘记这么明显的事情。谢谢! #include <iostream> #include <tuple> #include <typ
元组
,有几种建议。下面的代码片段从重载运算符中复制了Kenny的答案您可以在此处了解clang不编译它的原因:。看来,克朗的行为符合标准
为了解决这个问题,您可以定义
操作符,下面是一个使用integer_序列的示例:只想指出链接的答案并不能解决编译问题-它仍然失败。这只是打印元组的另一种技术。糟糕,问题当然是名称空间:(我怎么会忘记这么明显的事情。谢谢!
#include <iostream>
#include <tuple>
#include <type_traits>
// template <typename... T>
// std::ostream& operator<<(std::ostream& os, const std::tuple<T...>& tup);
template <size_t n, typename... T>
typename std::enable_if<(n >= sizeof...(T))>::type
print_tuple(std::ostream&, const std::tuple<T...>&)
{}
template <size_t n, typename... T>
typename std::enable_if<(n < sizeof...(T))>::type
print_tuple(std::ostream& os, const std::tuple<T...>& tup)
{
if (n != 0)
os << ", ";
os << std::get<n>(tup);
print_tuple<n+1>(os, tup);
}
template <typename... T>
std::ostream& operator<<(std::ostream& os, const std::tuple<T...>& tup)
{
os << "[";
print_tuple<0>(os, tup);
return os << "]";
}
int
main()
{
auto t = std::make_tuple(1, std::make_tuple(2, 3));
std::cout << t << std::endl;
}
clang++-mp-3.5 -std=c++11 print.cc
print.cc:19:10: error: call to function 'operator<<' that is neither visible in the
template definition nor found by argument-dependent lookup
os << std::get<n>(tup);
^
print.cc:20:7: note: in instantiation of function template specialization
'print_tuple<1, int, std::__1::tuple<int, int> >' requested here
print_tuple<n+1>(os, tup);
^
print.cc:27:7: note: in instantiation of function template specialization
'print_tuple<0, int, std::__1::tuple<int, int> >' requested here
print_tuple<0>(os, tup);
^
print.cc:35:19: note: in instantiation of function template specialization
'operator<<<int, std::__1::tuple<int, int> >' requested here
std::cout << t << std::endl;
^
print.cc:24:19: note: 'operator<<' should be declared prior to the call site
std::ostream& operator<<(std::ostream& os, const std::tuple<T...>& tup)
^