# Halting problem in C++

The halting problem relies on the fluidity of Turing machines. That is, a string can represent a machine.

Can you do the same for C++ on a modern computer?

Let's see my first attempt. Let bool h(string x, string y) be the purported function that decides haltingness and always halts. You can easily turn it into a full program.

Now define a self-contradictory function f:

bool f(string x) {
if (h(x, x))
for (;;);
return true;
}

int main() {
cout << h(f, f) << endl;
}


The problem is that I can't feed the code of f into h in main().

My second attempt: Prepare 4 files: h.cpp, h.exe, f.cpp, f.exe. And suppose the call formats are

h.exe filename1 filename2
f.exe filename


My problem is that in f.cpp I need to call h.exe, which in turn has to be part of f.cpp.

• Surely you can feed the code of f into h. Just write it again as a literal string. – Dmitri Urbanowicz Jul 12 at 10:11
• But also keep in mind, that then you either need to include the source of h into that string, or you need to be sure that h knows that "h(x,x)" means call of h itself. – Dmitri Urbanowicz Jul 12 at 10:12