Home print any struct in template function
Reply: 0

print any struct in template function

user9433 Published in September 21, 2018, 8:04 am

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;


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?

share|improve this question
  • How do you expect the print function to know how to print the contents? C++ doesn't have reflection (yet) – UnholySheep Feb 13 at 13:20
  • 1
    Look around for reflection. Unfortunately C++ doesn't have it natively (yet), but there are library implementations such as Boost.Fusion which generate the necessary metadata with a bit of user help. – Quentin Feb 13 at 13:21
  • You just want to use the same name or printing, or do you want the function to divine the contents of the structure like UnholySheep asked? – StoryTeller Feb 13 at 13:21
  • 1
    @Quentin can you explain about reflection? what is this? – Joo Feb 13 at 13:24
  • Probably duplicate of: stackoverflow.com/questions/19059157/… – dgrat Feb 13 at 13:25

2 Answers 2

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.382716 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO