2

I know that C++ is not decidable. But is it recursively enumerable?

Let's define the set of valid C++ programs to be any well defined program under the current C++ standards.

Is it possible to construct a compiler that can always identify valid C++ programs in finite time?

Or is it co-recursively enumerable?

Is it possible to construct a compiler that can always identify invalid C++ programs in finite time?

Or neither?

  • 3
    You might be looking for the [Theoretical Computer Science stackexchange site](http://cs.stackexchange.com/). This question is not in the least related to actual coding problems. – Ben Voigt Mar 15 '14 at 05:34
  • "C++ Templates are Turing Complete (2003)", by Todd L. Veldhuizen: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.14.3670 – rici Mar 15 '14 at 05:38
  • This question is not a practical programming question. It appears to be a theoretical one. But anyway, consider: `while (Riemann_hypothesis_is_true()) { }`. 1.10.24 says that a program must eventually terminate, call an I/O function, access a volatile object, perform a synchronization operation, or perform an atomic operation. If the Riemann hypothesis is true, then this program is invalid. – Raymond Chen Mar 15 '14 at 16:57
  • "The implementation may assume that" and "A valid program must" are different things. An implementation may assume that something is true, but just because it isn't doesn't mean the program is invalid. – Hans Mar 25 '14 at 01:57
  • BTW, if you have an implementation of Riemann_hypothesis_is_true I'd love to see it :P. – Hans Mar 25 '14 at 01:58

1 Answers1

2

Is it possible to construct a compiler that can always identify valid C++ programs in finite time?

Yes. Given enough time and resources a C++ compiler should be able to finish compiling any valid C++ program. A recursively enumerable language requires a turing machine that always terminates and provides a positive answer when the string is in the language, and the compiler does essentially that.

Is it possible to construct a compiler that can always identify invalid C++ programs in finite time?

No. The C++ template language is Turing complete, so you can write an infinite recursion in it. Due to the halting problem, it's impossible to determine whether or not a program will ever finish compiling, thus it's impossible to determine whether the C++ program would ever successfully compile.

I once wrote an infinite recursion in C++ templates and tried to compile with gcc. It turns out that gcc has a configurable recursion depth limit.

Hans
  • 2,230
  • 23
  • 23
  • Is an infinitely recursive template program invalid by my definition though? –  Mar 15 '14 at 05:40
  • Do the standards forbid infinite template metaprogramming? –  Mar 15 '14 at 05:43
  • Good question. Being a practical sort of spec (no formal semantics), I would imagine that the question isn't really addressed. – Hans Mar 15 '14 at 05:45
  • In the current draft I found this: "The result of an infinite recursion in instantiation is undefined." – Hans Mar 15 '14 at 05:48
  • So actually, neither question has a real answer. – Hans Mar 15 '14 at 05:48
  • 1
    That word "undefined" is used in the Standard to indicate invalid programs. – Ben Voigt Mar 15 '14 at 06:03
  • Hmm... but we're only looking at template recursion here. If we have two pointers p1 and p2. *p1 = (*p2)++; should be undefined if p1 == p2, but well-defined if p1 != p2. Could there be a compiler that can always prove p1 != p2, and accept the program? –  Mar 15 '14 at 06:23
  • We're getting into redefining the question again. When talking about strings in the C++ language if we try to consider whether or not the programs make sense or work properly then we're back to the halting problem. Template recursion is important because it's compile time processing. Since the compile time processing has to succeed in order for the program to compile, we need to consider it. Run time processing isn't an issue because we've already created the program by that point, and have the answer. The C++ standard defines which programs can compile, not which ones make sense. – Hans Mar 15 '14 at 16:15
  • If we define C++ based on practical compiler implementations, and every compiler has a template recursion depth limit, you could say that no program with infinite template recursion is valid. Since the C++ standard doesn't consider every edge case and leaves some constructs undefined, I think it makes sense to consider a specific implementation if we want to formally define the language. – DarthVlader Dec 28 '21 at 10:59