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

  • SEARCH
  • Home
  • 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 8789039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:17:34+00:00 2026-06-13T22:17:34+00:00

Possible Duplicate: Pretty-print std::tuple In a database library (soci), there is a chunk of

  • 0

Possible Duplicate:
Pretty-print std::tuple

In a database library (soci), there is a chunk of code below that works on std::tuple<>‘s from one to ten parameters.

static class methods from_base() and to_base() are implemented for arguments of 1-tuple to 10-tuple.

The guts essentially stream every n-tuple element to and from the passed-in stream. Everything is hard-coded.

How can this code be translated to use C++11’s variadic templates instead (no limit on parameters)?
Actually using variadic templates or not is secondary. What we really want to do is replace the hard-coding with the general case of n-tuple argument.

Part of the problem is, there is, technically, only one argument, but that argument is an n-tuple, so I can’t quite use what is described here in Wikipedia. What is the best approach?

#include "values.h"
#include "type-conversion-traits.h"
#include <tuple>

namespace soci
{

template <typename T0>
struct type_conversion<std::tuple<T0> >
{
    typedef values base_type;

    static void from_base(base_type const & in, indicator ind,
        std::tuple<T0> & out)
    {
        in
            >> std::get<0>(out);
    }

    static void to_base(std::tuple<T0> & in,
        base_type & out, indicator & ind)
    {
        out
            << std::get<0>(in);
    }
};

template <typename T0, typename T1>
struct type_conversion<std::tuple<T0, T1> >
{
    typedef values base_type;

    static void from_base(base_type const & in, indicator ind,
        std::tuple<T0, T1> & out)
    {
        in
            >> std::get<0>(out)
            >> std::get<1>(out);
    }

    static void to_base(std::tuple<T0, T1> & in,
        base_type & out, indicator & ind)
    {
        out
            << std::get<0>(in)
            << std::get<1>(in);
    }
};

// ... all the way up to 10 template parameters

}

RUNNABLE ANSWER (based on Grizzly’s post below)

#include <iostream>
#include <tuple>

using namespace std;

// -----------------------------------------------------------------------------

template<unsigned N, unsigned End>
struct to_base_impl 
{
    template<typename Tuple>
    static void execute(Tuple& in, ostream& out) 
    {
      out << std::get<N>(in) << endl;
      to_base_impl<N+1, End>::execute(in, out);
    }
};

template<unsigned End>
struct to_base_impl<End, End>
{ 
    template<typename Tuple>
    static void execute(Tuple& in, ostream& out) 
    {
      out << "<GAME OVER>" << endl;
    }
};

// -----------------------------------------------------------------------------

template <typename Tuple>
struct type_conversion
{
    static void to_base(Tuple& in, ostream& out )
    {
        to_base_impl<0, std::tuple_size<Tuple>::value>::execute(in, out);
    }
};

template <typename... Args>
struct type_conversion<std::tuple<Args...>>
{
    static void to_base(std::tuple<Args...>& in, ostream& out )
    {
        to_base_impl<0, sizeof...(Args)>::execute(in, out);
    }
};

// -----------------------------------------------------------------------------

main()
{
    typedef tuple<double,int,string> my_tuple_type;
    my_tuple_type t { 2.5, 5, "foo" };

    type_conversion<my_tuple_type>::to_base( t, cerr );
}
  • 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-06-13T22:17:35+00:00Added an answer on June 13, 2026 at 10:17 pm

    If I understand your question correctly you basically need to call the operator << or >> on in respectively out for every element of your tuple. In this case you can construct something similar to a for loop using partial specialization and recursion (sort of, since it actually calls a different function each time):

    template<unsigned N, unsigned End>
    struct to_base_impl {
        template<typename Tuple>
        void execute(Tuple& in, base_type& out) {
          out<<std::get<N>(in);
          to_base_impl<N+1, End>::execute(in, out);
        }
    };
    
    template<unsigned End>
    struct to_base_impl<End, End> { //End of loop
        template<typename Tuple>
        void execute(Tuple& in, base_type& out) {}
    };
    
    template <typename Tuple>
    struct type_conversion
    {
        typedef values base_type;
    
        static void to_base(Tuple& in, base_type & out, indicator & ind){
            to_base_impl<0, std::tuple_size<Tuple>::value>::execute(in, out);
        }
    };
    

    This will iterate from zero to the size of the tuple and call out<<std::get<N>(in); each iteration. from_base would be implemented the same way only with in>>std::get<N>(out);. If you want to make sure your converter is only called with tuples you can use a variadic template:

    template <typename... Args>
    struct type_conversion<std::tuple<Args...>>
    {
        typedef values base_type;
    
        static void to_base(std::tuple<Args...>& in, base_type & out, indicator & ind){
            to_base_impl<0, sizeof...(Args)>::execute(in, out);
        }
    };
    

    Of course you could write this more generically, but that will make the source much more complex and probably not give you much in return.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: JSON pretty print using JavaScript I'm working on a project that will
Possible Duplicate: Look and feel in java is there a third party pretty L&F
Possible Duplicate: Javascript library: to obfuscate or not to obfuscate - that is the
Possible Duplicate: Auto-generation of .NET unit tests I am pretty new to Unit Testing
Possible Duplicate: C++: When to use References vs. Pointers I'm pretty new programmer to
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: Objective C for Windows iPhone development on Windows Is there any way
Possible Duplicate: JavaScript Variable Scope I am (obviously) pretty new to Javascript, and I've
Possible Duplicate: Why should I capitalize my SQL keywords? hi, I'm pretty new to
Possible Duplicate: The ultimate clean/secure function I was informed in another thread that this

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.