Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading output << operator for a class to print the tuple inside it [duplicate]

So If I have something like this

template<typename... Args >
class tuple_class
{
  public:
  std::tuple<Args...> tup;

/*I left out the other functions */
};

I want to overload the operator<< so that it will recursively print the tuple when called on the class.

ex.

auto a = tuple_class(1, 2 ,3);
std::cout << a << endl;

would hopefully print '123'

Ive seen other examples of tuple printers but I can't apply it to my class without having a bunch of trouble

I think I should start with a member function like this

  template<typename... Args>
  friend std::ostream& operator<<(std::ostream& os, const my_tuple<Args...> &m);

and then the actual function outside the class

template<typename... Args>
std::ostream& operator<<(std::ostream& os, const my_tuple<Args...> &m)
{
   os << "SOMETHING" << std::endl;
   return os;
}

That actually worked when I call the << operator on my class. But I have no clue how to make it actually print the tuple.

Any help would be appreciated

like image 860
user2770808 Avatar asked May 16 '16 19:05

user2770808


People also ask

Can we overload << operator?

We can overload the '>>' and '<<' operators to take input in a linked list and print the element in the linked list in C++. It has the ability to provide the operators with a special meaning for a data type, this ability is known as Operator Overloading.

How do you overload operators in Python?

In Python, overloading is achieved by overriding the method which is specifically for that operator, in the user-defined class. For example, __add__(self, x) is a method reserved for overloading + operator, and __eq__(self, x) is for overloading == .

What is function overloading and operator overloading in Python?

The operator overloading in Python means provide extended meaning beyond their predefined operational meaning. Such as, we use the "+" operator for adding two integers as well as joining two strings or merging two lists. We can achieve this as the "+" operator is overloaded by the "int" class and "str" class.

What are overloaded operators in C++?

An overloaded operator is called an operator function. You declare an operator function with the keyword operator preceding the operator. Overloaded operators are distinct from overloaded functions, but like overloaded functions, they are distinguished by the number and types of operands used with the operator.


1 Answers

To build solution I used tuple print code from cppreference mentioned here this. Rest of the code is glue to keep things together. Here I put working sample.

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

// tuple printer
template<class Tuple, std::size_t N>
struct TuplePrinter {
    static std::ostream& print(std::ostream& os, const Tuple& t)
    {
        TuplePrinter<Tuple, N - 1>::print(os, t);
        os << ", " << std::get<N - 1>(t);
        return os;
    }
};

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

template<class... Args>
std::ostream& print(std::ostream& os, const std::tuple<Args...>& t)
{
    os << "(";
    TuplePrinter<decltype(t), sizeof...(Args)>::print(os, t);
    os << ")\n";
    return os;
}


// class to keep tuple inside
template<typename... Args>
class tuple_class {
    template<typename... Args2>
    friend std::ostream& operator<<(std::ostream& os, const tuple_class<Args2...> &m);

    std::tuple<Args...> tup;
public:
    tuple_class(Args&&... args) : tup(std::forward<Args>(args)...) {

    }
};

// usage of the printer
template<typename... Args>
std::ostream& operator<<(std::ostream& os, const tuple_class<Args...> &m) {
    print(os, m.tup);
    return os;
}



int main() {
    tuple_class<int,float,std::string> tc( 1,3.0f,"string" );
    std::cout << tc;
    return 0;
}
like image 67
LookAheadAtYourTypes Avatar answered Oct 26 '22 00:10

LookAheadAtYourTypes