Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7490407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:35:56+00:00 2026-05-29T15:35:56+00:00

I used an answer to the SO question iterate over tuple to write a

  • 0

I used an answer to the SO question “iterate over tuple” to write a method to overload <<. This method was tested and appears to work correctly with g++ 4.7 on Debian squeeze.

However this method is kind of roundabout, since it seems << cannot be explicitly instantiated (I found a post about it
here). So, one is forced to define a string method and then call that. I have a similar method for vector, which is more direct. Does anyone have suggestions about how to eliminate the extra step of creating a string method, using the same approach, or otherwise? Thanks in advance.

#include <tuple>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using std::ostream;
using std::cout;
using std::endl;
using std::vector;
using std::string;

// Print vector<T>.
template<typename T> ostream& operator <<(ostream& out, const vector<T> & vec)
{
  unsigned int i;
  out << "[";
  for(i=0; i<vec.size(); i++)
    {
      out << vec[i];
      if(i < vec.size() - 1)
    out << ", ";
    }
  out << "]";
  return out;
}

////////////////////////////////////////////////////////////////

// Print tuple.
template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I == sizeof...(Tp), string>::type
stringval(const std::tuple<Tp...> & t)
{
  std::stringstream buffer;
  buffer << "]";
  return buffer.str();
}

template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I < sizeof...(Tp), string>::type
stringval(const std::tuple<Tp...> & t)
{
  std::stringstream buffer;
  size_t len = sizeof...(Tp);
  if(I==0)
      buffer << "[";
  buffer << std::get<I>(t);
  if(I < len - 1)
    buffer << ", ";
  buffer << stringval<I + 1, Tp...>(t);
  return buffer.str();
}

template<typename... Tp> ostream& operator <<(ostream& out, const std::tuple<Tp...> & t)
{
  out << stringval(t);
  return out;
}

int
main()
{
  typedef std::tuple<int, float, double> T;
  std::tuple<int, float, double> t = std::make_tuple(2, 3.14159F, 2345.678);
  cout << t << endl;
}

When compiled, this gives

[2, 3.14159, 2345.68]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:35:57+00:00Added an answer on May 29, 2026 at 3:35 pm

    You can just pass the std::ostream& into that stringval function and use out << instead of buffer <<.

    Demo:

    #include <tuple>
    #include <iostream>
    #include <type_traits>
    
    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 << "]";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used the following script to try to answer this question: def isEquilateral(x, y,
Hello i used of answer in this question : Create new class from a
I've used answer from this question: Django: making raw SQL query, passing multiple/repeated params?
I used this question/answer to install DB2 in Lion: How do I install IBM
This question came into my mind while generating sample data for a SO-answer. I
( This question , though similar, didn't really answer my question.) I've had problems
This question is a follow up to: Why can’t I call a method outside
hi i have created a question answer based app, the question's used to be
This question is a result of what i noticed while trying to answer another
Trying to answer this question , I created this Python regular expression to match

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.