5 ms·
> we could imagine a future version of C++ where this [constexpr annotation] isn’t required: if we’re calling a function at compile-time, the compiler tries to
by WalterBright 5y ago
> we could imagine a future version of C++ where this [constexpr annotation] isn’t required: if we’re calling a function at compile-time, the compiler tries to execute it at compile-time. If it works, good, otherwise, it issues a diagnostic.
This is how D's CTFE (Compile Time Function Execution) works. You can execute at compile time any function, and it will work as long as the source code is available to the compiler, and it doesn't access globals or do pointer monkey business.
You can indeed build a program that runs completely at compiler time. Here's a compile time ray tracer in D circa 2006:
http://h3.gd/posts/ctrace/ http://h3.gd/posts/ctrace/
- codetrotter 5y agoJonathan Blow’s programming language allows almost anything to execute during compilation as well. It’s neat. Here’s an old video where he demonstrated this: https://youtu.be/UTqZNujQOlA https://youtu.be/UTqZNujQOlA
- WalterBright 5y agoThat's true. His implementation also allows compile time function execution to make system calls. D disallows that in order to prevent malicious source code from being distributed and tricking people into compiling it. There is a mechanism to sent output to stdout, input can be read from a file residing on the compiler's system, and that's it. It's also why CTFE doesn't allow pointer monkey business, like coercing an integer into a pointer and dereferencing it.
- SubjectToChange 5y agoIf you're compiling malicious code, haven't you already lost? I don't see how preventing pointer manipulation at compile time (or anything else, really) improves security unless people never run the executable they compiled.
- WalterBright 5y agoPeople expect to check source code before running it. They do not expect to have to check it before compiling it. Malware works by taking advantage of peoples' expectations.
- SubjectToChange 5y agoPeople expect to check source code before running it. They do not expect to have to check it before compiling it. Why are they compiling untrusted code? To see if it's syntactically correct? If the code you're compiling is using a build system, you're already compromised. Compilation of untrusted code should always be sandboxed, regardless of compile-time programming capabilities.
- gpderetta 5y agoGiven the way modern IDEs work, unrestricted compile time evaluation would make it dangerous to even open potentially malicious code.
- lalaithion 5y agoIt means that you need to do $ sandbox "foobarc malicious.foobar -o executable && ./executable" instead of $ foobarc malicious.foobar -o executable $ sandbox ./executable Which isn't totally intuitive.
- WalterBright 5y agoYes, I don't really want to have to recommend to people that they have to run the compiler in a sandbox.
- SubjectToChange 5y agoAnd what if you are compiling more than a single file? What if the project or library you are compiling uses make/cmake/gn/etc? The point is that restrictions on compile-time programming only protect you in trivial cases. And I can't think of many reasons to fully compile code without the intent of running the executable.
- rurban 5y agoIn Perl we only discourage IO at compile-time, because this only runs at the build system, not the client. And IO is mostly side effects only, and not restorable. Everything else is free. In perl this is simpler done, everything inside BEGIN{} blocks.
- kubb 5y agoWhat a novel idea, I'm sure there wasn't a language called lisp that could do that 50 years ago.
- WalterBright 5y agoLisp does deserve credit for being first. But didn't Lisp do it the other way around - interpreter first, later add native code generation? Before D, people discovered that C++ templates could be used for compile time execution. Everyone was excited about this. The book that revolutionized C++ programming by exploiting this was Andrei Alexandrescu's book "Modern C++ Design" published in 2001. But as far as I knew, nobody ever wrote about executing regular C or C++ functions at compile time in a native compiler. (There were some C interpreters, but they were interpreters only.) Once D showed it could be done, and how useful it was, the rush was on for other curly braced languages to do it.
- secondcoming 5y ago> Andrei Alexandrescu Who's that guy?
- johnnycerberus 5y agoBasically one of the gods of the C++ pantheon.
- moonchild 5y agoIt's possible that very early lisps were interpreted—I'm not sure—but lisp had been a predominantly compiled language for many years before c++ arrived on the scene.
- deleted 5y ago[deleted]
- omegalulw 5y agoI don't see the value TBH. constexpr is excellent for code readability. Even if the compiler could figure it out I'd vote to keep it. That said, if compiler could figure it out than you can probably set up your editor to annotate it.
- WalterBright 5y agoAs the article points out, an entire function doesn't need to be CTFE compatible, only the particular path taken through the function by the interpreter. This makes constexpr more of an impediment than an enabler. Besides, people soon discover the utility of using functions at both compile and run time, and nobody has ever asked for D to require an annotation for it in 15 years.
- SubjectToChange 5y agoI don't see how constexpr is helpful for readability. As it stands, constexpr adds a significant amount of syntactic noise. D's approach is the correct one, IMO.
- pjmlp 5y agoCheck Circle for what would be possible in C++, if the compiler would do it for you. https://www.circle-lang.org/ https://www.circle-lang.org/
- titzer 5y agoVirgil initializers are run at compile time and can literally run any code in the program to build up any data structures you want. The result of initialization is the transitive closure of objects reachable from the globals. The compiler goes further and optimizes your code against the initialized heap, devirtualizing, inlining, and constant-folding, throwing away dead code, dead fields, and dead objects. That's great for microcontrollers because you can just allocate your entire heap at compile time and then bake it into your image. I wrote a paper about it in 2006: https://escholarship.org/uc/item/13r0q4fc https://escholarship.org/uc/item/13r0q4fc I still work (primarily, in fact) on Virgil. It's great not having any distinction between compile-time initialization and runtime.
- pjmlp 5y agoAnd Circle, which is a C++ compiler with an added annotation,@meta. https://www.circle-lang.org https://www.circle-lang.org There are some issues with it, because many ISO members want compile time capabilities constrained by constexpr, instead of the complete language, like Circle compiler allows.