By Joo


2018-02-13 13:17:56 8 Comments

I have 3 or more struct , and I want that I have one function for print any struct

for example :

struct A 
{
  int a0;
  string a1;
  bool a2;
}

and

struct B
{
 CString b0;
 double b1;
 int b2;
 string b3
}

I want print this struct ( A and B) with same function

like this :

template<typename T>
inline void print(T)
{
  std::cout << // I don't know what is write here....
}

any body help me?

2 comments

@user9335240 2018-02-13 13:47:26

Also, another solution is to make a function called to_string for example (this is to directly convert your struct to string, but the performance is not good)

struct A
{
  int a0;
  string a1;
  bool a2;

  string to_string() const {
      return "{ " + std::to_string(a0) + ", " + a1 + ", " + (a2 ? "true" : "false") + " }";
  }
};

Then

template<typename T>
void print(const T &a) {
    cout << a.to_string() << "\n";
}

This is not good for printing, for printing, use the C++ convention of implementing operator<<

But.

This have a disadvantage, when an subclass want to change the output format. It can't.

So

class DynamicallyToStringConvertible {
public:
    virtual string to_string() const = 0;
    virtual ~DynamicallyToStringConvertible() {}
};

struct A: DynamicallyToStringConvertible
{
    ...
    virtual string to_string() const {
        return "{ " + std::to_string(a0) + ", " + a1 + ", " + (a2 ? "true" : "false") + " }";
    }
}

struct SubclassFromA: public A {
    virtual string to_string() const {
        return "Subclass: " + A::to_string();
    }
};

The function print is as is

Then

void tryPrintingAsA(const A &a) {
    print(a);
}

You will find now (after virtual functions), this will work for SubclassA. But if you tried the operator<< or the beginning of the solution (to_string without virtual), it will work for SubclassA as it was a vanilla A.

Then you would make something like that

friend ostream &operator<<(ostream &s, const DynamicallyToStringConvertible &p) {
    return s << p.to_string();
}

Inside your Base class, like this:

class DynamicallyToStringConvertible {
public:
    virtual string to_string() const = 0;
    virtual ~DynamicallyToStringConvertible() {}

    friend ostream &operator<<(ostream &s, const DynamicallyToStringConvertible &p) {
        return s << p.to_string();
    }
};

Now try the printing function for A to be:

void tryPrintingAsA(const A &a) {
    cout << a;
}

@Ivan 2018-02-13 13:21:15

Usual practice in C++ is to define operator<<(std::ostream &, const T &) for your type:

std::ostream &operator<<(std::ostream &os, const A &value)
{
    // print here
    return os;
}

This should be done for each type you want to print and this function should be defined in the same namespace as that type.

After that, your data types can be printed to all output stream. This also allows things like boost::lexical_cast to work with your type as it prints value to std::stringstream internally.

@Joo 2018-02-13 13:26:35

can you show me, how can use this?

@Ivan 2018-02-13 13:27:58

@Joo Manually for each data type. Were asking for how to iterate over all fields? Because I don't see that being mentioned in the question.

@Ivan 2018-02-13 13:30:27

@Joo If you actually need to print it (e.g. need this for debugging or logging), then I strongly suggest you doing this manually. Custom printing can be way more useful than something automatically generated.

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

23 Answered Questions

[SOLVED] When should you use a class vs a struct in C++?

  • 2008-09-10 16:29:54
  • Alan Hinchcliffe
  • 305207 View
  • 696 Score
  • 23 Answer
  • Tags:   c++ oop class struct ooad

13 Answered Questions

[SOLVED] Why can templates only be implemented in the header file?

28 Answered Questions

[SOLVED] When to use struct?

  • 2009-02-06 17:37:55
  • Alex Baranosky
  • 216748 View
  • 1181 Score
  • 28 Answer
  • Tags:   c# struct

10 Answered Questions

[SOLVED] Storing C++ template function definitions in a .CPP file

  • 2008-09-22 15:55:52
  • Rob
  • 230754 View
  • 363 Score
  • 10 Answer
  • Tags:   c++ templates

8 Answered Questions

[SOLVED] Difference between 'struct' and 'typedef struct' in C++?

  • 2009-03-04 20:41:12
  • criddell
  • 411263 View
  • 660 Score
  • 8 Answer
  • Tags:   c++ struct typedef

12 Answered Questions

[SOLVED] typedef struct vs struct definitions

  • 2009-11-04 17:21:57
  • user69514
  • 499337 View
  • 618 Score
  • 12 Answer
  • Tags:   c struct typedef

5 Answered Questions

7 Answered Questions

[SOLVED] Pretty-print C++ STL containers

6 Answered Questions

[SOLVED] How to print function pointers with cout?

  • 2010-01-14 14:22:26
  • ibread
  • 36903 View
  • 41 Score
  • 6 Answer
  • Tags:   c++

Sponsored Content