0

Is it possible to convert a raw string to code in c++?

For example I have a string

string s = "cout << "Hello World";";

I want to generate that code programatically. Maybe i want to insert that code in various parts of the program dynamically.

cout << "Hello World";

Also, is it possible to generate code using loops in c++ maybe using pre-processor directives?

animuson
  • 53,861
  • 28
  • 137
  • 147
user3059427
  • 209
  • 2
  • 3
  • 10
  • 2
    Perhaps I'm reading it wrong, but I don't believe he's asking about functions. I think he wants, for example, to query the user for a string, have the user type in some C++ code, and then run that code. – JBentley Jan 18 '14 at 22:02
  • @JBentley Thanks for clarification. That is what i was asking. So, that i can pass code as argument and run it. Lets say inside the function i check the first few characters of the passed in string and if it matches the condition then convert that passed in string to code and run it. – user3059427 Jan 18 '14 at 22:06
  • To let a user enter arbitrary code and then run it, you would usually use a C++ interpreter. I don't know of any. You might look at LLVM to compile code and run it in your process. – brian beuning Jan 18 '14 at 22:06

2 Answers2

4

It looks like you want something akin to the eval function provided by Lisp, Python, perl, and many other languages.

There is no such capability in C++. It just doesn't exist, by design.

One way to get around this missing functionality is to make your program write a program, store it in a file, invoke the compiler, and invoke the executable generated by the compiler. That's rather ugly. Anything you do is going to be rather ugly.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
2

Is it possible to convert a raw string to code in c++?

C++ is typically a compiled language. This makes what you're trying to do tricky. In order to achieve what you're saying, you'd need to either have the code interpreted at runtime (in which case you'd be better served by binding to a scripting language such as Python or Lua), or embed or call a C++ compiler and pass the code to that, then run it.

It is possible that this is a XY problem and that the solution you are looking at is overly complicated for the actual root problem you are trying to solve. It may be an idea to put up a description of why you're trying to do this, as there may be a much simpler way to achieve the end result you want.

Community
  • 1
  • 1
JBentley
  • 6,099
  • 5
  • 37
  • 72
  • Downvoter care to explain so I can modify my answer if necessary? – JBentley Jan 18 '14 at 22:13
  • 2
    "C++ is a compiled language." - please provide a standard quote that proves this sentence. – Griwes Jan 18 '14 at 22:13
  • 1
    I think should remove "C++ is a compiled language", then it's a good answer. – Steve M Jan 18 '14 at 22:19
  • 1
    I think "C++ is a compiled language" should be kept. That's what section 2.2 of the standard is all about. The problem is the statement "Is it possible to convert a raw string to code in C++? No, this is not possible." Compilers do just that. – David Hammen Jan 18 '14 at 22:21
  • @DavidHammen Since there are C++ interpreters out there, even that assertion isn't correct, but I agree that "no it's not possible" is more problematic. –  Jan 18 '14 at 22:22
  • How about "C++ is usually a compiled language"? – Steve M Jan 18 '14 at 22:23
  • @DavidHammen "C++ is a compiled language" is a claim backed by nothing **and** proven false by at least two interpreters I've heard of, henceforth it should *not* be kept. 2.2 can be applied to either a whole file or just to a single line or statement, and doesn't contradict with "C++ is an interpreted language". – Griwes Jan 18 '14 at 22:24
  • @JBentley Alright, now this is a version I can agree with. – Griwes Jan 18 '14 at 22:25
  • @Griwes - Are those interpreters fully compliant, or do they just handle a subset of the language? C++ was certainly designed to be a compiled language. – David Hammen Jan 18 '14 at 22:29
  • The "C++ is a compiled language" is a bit of a red herring. Java and Python are "compiled languages" in the sense that both are compiled to some intermediate language prior to execution. Python has an eval statement built-in; in Java one can pull in the interpreter and get the equivalent of Python's eval. There is no eval in C or C++, there is no built-in support for interpreting on the fly, and there is no built-in support for executing newly generated code. You could do it in C/C++, but it would be ugly and non-portable. – David Hammen Jan 18 '14 at 22:37