4

Is C++ a turing complete language?

Obviously that would be the case, but how is it proven practically?

Is there a minimally reproducible example that shows that this is the case?

  • 2
    In order to do so, you would need to prove, that C++ is at least as powerful as a turing machine. One way to go, would be to write a turing machine in C++, having the program emulate a turing machine. – Julian Kirsch Aug 02 '20 at 14:16
  • 1
    "nearly all programming languages are Turing complete if the limitations of finite memory are ignored." – bolov Aug 02 '20 at 14:19
  • and some non programming languages are Turin complete. E.g. [minesweeper is Turing complete](https://en.wikipedia.org/wiki/Turing_completeness#cite_note-10) - mind cell blown – bolov Aug 02 '20 at 14:35
  • Turing Complete is not a very high bar. Add a second stack to a pushdown automata and its Turing Complete. Add two counters to a NFA and it’s Turing Complete. I am not sure if folks know what this means. It is almost trivial to create a Turing Complete language. – wcochran Aug 02 '20 at 14:40
  • @bolov A Turing machine has arbitrarily large, not infinite, memory. (Since a computation must terminate in a finite amount of time, it can only need a finite amount of memory.) I don't know where this idea of infinite memory comes from, but it's certainly not Turing. – molbdnilo Aug 02 '20 at 14:55
  • 1
    @bolov • I presume "Turin complete" is a typo, because Turin complete programming languages are shrouded in mystery. – Eljay Aug 02 '20 at 16:04
  • my friends if there's any language that's Turing complete, it's C++ – KeyC0de Sep 01 '23 at 15:46

2 Answers2

4

Yes it is, from wikipedia Turing completeness

To show that something is Turing-complete, it is enough to show that it can be used to simulate some Turing-complete system. For example, an imperative language is Turing-complete if it has conditional branching (e.g., "if" and "goto" statements, or a "branch if zero" instruction; see one-instruction set computer) and the ability to change an arbitrary amount of memory (e.g., the ability to maintain an arbitrary number of data items).

Then imperative languages lists C++ as such.

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
  • 6
    It should be noted that **technically** speaking, *C++ the language* is Turing complete, while any particular *implementation* (such as "`g++` on machine X") is not. – bitmask Aug 02 '20 at 14:22
  • @bitmask Are you saying it can be made turing-complete by giving it infinite memory? C++ requires pointers to have finite size, so it might be impossible. – HolyBlackCat Aug 02 '20 at 14:55
  • 3
    @HolyBlackCat Not infinite, but arbitrarily large. So, although, **yes**, you cannot have an infinite amount of memory, you can at least have an arbitrary amount of it. So, it'd be conceivable to instantiate program X with 2^k bits of pointer-width until you run out of memory, then start again from scratch with 2^(k+1) bits, and so forth. Given that any program that **eventually halts** only uses a **finite amount** of memory for **that run**, you don't need infinite memory, you just need to be able to supply more memory indefinitely. In contrast, "g++ on X" has a fixed amount of memory. – bitmask Aug 02 '20 at 15:16
  • 3
    ... however, the C++ standard *does* speak about *implementations*, so the abstract language cannot be fully decoupled from an actual implementation. Oh well, if we use the highly formal method of *squinting* C++ certainly *looks* Turing complete :) – bitmask Aug 02 '20 at 15:19
-2

I'm not an expert of computational theory but as empirical law a language is declared Turing-complete if it supports conditional branching, i.e. it must support if statements and go-to instruction. So the majority of languages out there is Turning-complete.

Ref. https://en.m.wikipedia.org/wiki/Turing_completeness

Andrea Del Bene
  • 2,521
  • 1
  • 15
  • 20
  • 2
    This is wrong: `goto`s are not **conditional** branches (conditional gotos exist in some languages, but are not the typical case) and a language that supports `if` statements as its sole control-flow statement is not turing-complete is it cannot emulate the simple `while (true) { }` program. Having `if` **and** `goto` is enough. Basically, you need some way to repeat previous statements and some way to decide behavior based on input. – danielschemmel Aug 02 '20 at 14:31
  • In fact, just `while` is enough since `while` can easily simulate `if`. – Patrick87 Aug 03 '20 at 12:09