0

I want to print all declared variables along with their types in my program.

#include<iostream>
int main(){
int a; 
char b;
float c;
}

My program should out put

The Variables in the program: 'int a', 'char b' & 'float c'

  • You need to write a C++ parser (a daunting task for anyone) or use somebody else's. [libclang](http://clang.llvm.org/docs/Tooling.html) is popular for analysis of source code. – molbdnilo Jan 09 '17 at 09:14
  • Do you want to print this from source files? And for anything more than a small program this is extremely impractical... so what is your ***real*** problem? – Disillusioned Jan 09 '17 at 09:15
  • @molbdnilo The poor lad just started programming and you throw him into the snake pit already, "write a C++ parser." – Hatted Rooster Jan 09 '17 at 09:30
  • @CraigYoung Initially I had been thinking on ways to print all variables in a file imported in a program. But taking a step by step approach, I am thinking on printing all the variables within the program at first. – MarshallCoder Jan 09 '17 at 09:38
  • @SourabSharma What do you mean by "file imported in a program"? I don't think you understand the scale of the problem you think is a "step by step approach". The layout of symbols in a source-code file is **not** trivial. Writing a program to understand this does not amount to an easy task. You also seem to think a program is always a single file; but maybe I'm just misunderstanding you. Your question is either: too broad, unclear or doesn't have sample code. (***No! The code you show doesn't count because that's not the code that does the task you're asking about.***) – Disillusioned Jan 09 '17 at 09:59
  • @CraigYoung No You are not getting my point. Let me come on this question again from the beginning. Say I have a program with a **few variables declared**. I want my program to read all the declared variables and spit the declared variables along with their types as output.Which seems difficult unless another program parses it. Before approaching this forum, I had been thinking on one.cpp program which includes "another.cpp" in one.cpp and printing all variables in another.cpp along with types. So by "step by step approach" i meant first nudging with in the program and then going to the nxt. – MarshallCoder Jan 09 '17 at 10:47
  • @CraigYoung It doesn't have a sample code. I have been exploring C++ deeper while working on Data structures.So this is just a randomly thought question. – MarshallCoder Jan 09 '17 at 10:50
  • @SourabSharma If you'd been thinking in terms of parsing (without stating it) from the start, then this Q is definitely too broad. But I still think you're neglecting the fundamental complexity of what you're asking... Even in a small program you get multiple methods and local variables within each. Global variables in each file. Class and struct members. Nested scopes, namespaces.... Have you thought about how to differentiate all these? SO is not a code writing service. Only you know what you're trying to achieve, so get started. If you get stuck you'll have a suitable _specific_ question. – Disillusioned Jan 09 '17 at 11:22
  • My question must be too difficult to understand. But It is as simple as in the description above **I want to print the declared variables and not the values of the variables.** I am very much aware of nested scopes, namespaces, classes and global variables. To print a value of a variable from another program I can use extern, but here it is not that case.With all due respect, I am very much aware that SO is not a code writing service. I am looking for a solution very much like you did when you got started on SO. Regards. – MarshallCoder Jan 09 '17 at 11:37
  • The expected answer doesn't even match the example program: at least `std::cout`, `std::cin`, `std::cerr` and `std::clog` are missing. `` probably defines additional variables, but certainly those 4 must be included. – MSalters Jan 09 '17 at 12:03
  • @SourabSharma ***I'm sure it's not the case...*** But if that's all you want: `std::cout << "int a; char b; float c;\n";` And this is exactly why I've been spending my time asking questions to find the middle ground between trivially easy and excessively broad.... _Just trying to help_. (PS: With all due respect, you have **absolutely zero idea** what got me started on SO - so don't be so presumptuous. :) ) – Disillusioned Jan 09 '17 at 12:12

1 Answers1

4

You can't do this automatically in C++ since the C++ standard allows for all variable names to be compiled out in the final binary.

In this sense C++ is not a reflective language, unlike Java.

There are some tricks, but they rely on macros &c. See How can I add reflection to a C++ application?

Community
  • 1
  • 1
Bathsheba
  • 220,365
  • 33
  • 331
  • 451