4

Since I discovered boost::lexical_cast all conversions are a breeze. That's until trying to convert a tuples elements into a string. Like Int2String or Double2String I want a way to generate a single string from a tuple of arbitrary number of elements

Since the subject of conversion has arbitrary (yet compile time known) dimension, after some research I've looked into boost::fusion and found this solution :

#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/noncopyable.hpp>
#include <boost/fusion/include/for_each.hpp>

template <class Sequence>
std::string StringFromSequence(const Sequence &seq) 
{
    std::string result;
    boost::fusion::for_each(seq, toString(result));
    return result;
}

Where toString is a functor applying the lexical cast to the objects that's been called for :

struct toString: boost::noncopyable 
{
    explicit toString(std::string& res) : result(res)
    {
    }
    template <class T>
    void operator()(const T& v) 
    {
        result += boost::lexical_cast<std::string>(v);
    }
private:
    std::string &result;
};

When trying to use that though

std::tuple<int, char, double> tup{ 1, 'a', 2.2 };
toString(tup);

I get a compilation error

error C2893: Failed to specialize function template 'enable_if, void>::type boost::fusion::for_each(const Sequence &,const F &)'

  1. Can someone spot and correct the error ?
  2. Are there any (maybe boost free) alternatives ? (it would take compile time recursion and I was hopefully trying to avoid all that boilerplate code)
Community
  • 1
  • 1
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153

2 Answers2

3

Since this question is tagged C++11, here's my take at it:

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

template<typename T, T...>
struct integer_sequence { };

template<std::size_t N, std::size_t... I>
struct gen_indices : gen_indices<(N - 1), (N - 1), I...> { };
template<std::size_t... I>
struct gen_indices<0, I...> : integer_sequence<std::size_t, I...> { };

template<typename H>
std::string& to_string_impl(std::string& s, H&& h)
{
  using std::to_string;
  s += to_string(std::forward<H>(h));
  return s;
}

template<typename H, typename... T>
std::string& to_string_impl(std::string& s, H&& h, T&&... t)
{
  using std::to_string;
  s += to_string(std::forward<H>(h));
  return to_string_impl(s, std::forward<T>(t)...);
}

template<typename... T, std::size_t... I>
std::string to_string(const std::tuple<T...>& tup, integer_sequence<std::size_t, I...>)
{
  std::string result;
  int ctx[] = { (to_string_impl(result, std::get<I>(tup)...), 0), 0 };
  (void)ctx;
  return result;
}

template<typename... T>
std::string to_string(const std::tuple<T...>& tup)
{
  return to_string(tup, gen_indices<sizeof...(T)>{});
}

int main(int argc, char** argv)
{
  std::tuple<int, double, float> tup(1, 2.1, 3.2);
  std::cout << to_string(tup) << std::endl;
}

If you want to stick with boost::lexical_cast, replace to_string with lexical_cast.

Live output on ideone

Tom Knapen
  • 2,277
  • 16
  • 31
  • When I have a `std::string` in my tuple, I get this error: `error: no matching function for call to β€˜to_string(const std::__cxx11::basic_string&)’ s += to_string(std::forward(h)); ` Any ideas how to fix it? – ph_0 Apr 05 '22 at 10:24
3

Sorry but I'm too lazy to enter the details of where you're making the mistakes, but here's a solution using fusion and C++14 polymorphic lambdas:

#include <tuple>
#include <string>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/fusion/adapted/std_tuple.hpp> // you're missing this huh? it's needed to use fusion with std::tuple
#include <boost/fusion/algorithm/iteration/for_each.hpp>

int main() {
    using namespace std;
    using namespace boost::fusion;

    string result;
    for_each(make_tuple(1, 'a', 2.2), [&result](auto &s) {
        result += boost::lexical_cast<string>(s) + ' ';
    });

    cout << result << endl;
}

http://coliru.stacked-crooked.com/a/f110238a317eede9

oblitum
  • 11,380
  • 6
  • 54
  • 120