4 ms·
The problem here is that C++'s metaprogramming facilities have leaked into the parser to the extent that it's undecidable whether to parse that final declaratio
by roca 6y ago
The problem here is that C++'s metaprogramming facilities have leaked into the parser to the extent that it's undecidable whether to parse that final declaration as a variable declaration or a function declaration.
So: arguably the grammar is decidable, but to parse C++ you need more than the grammar, you need the full metaprogramming machinery. That is, C++ has ended up in the same state as a language that simply provides Turing-complete macros.
- tines 6y agoThis is what I love about C++ that isn't provided by [m]any of the "modern" languages (not including Lisp). I want programs in my programs dangit!
- roca 6y agoIf you really want that, it seems better to provide them properly like Rust proc-macros than to provide them in C++'s awful half-baked form.
- saagarjha 6y agoRust’s compile-time meta programming is lacking at the moment, unfortunately. It doesn’t have const generics yet, and the standard library is a bit anemic and this extends to the type programming utilities as well.
- adev_ 6y ago> If you really want that, it seems better to provide them properly like Rust proc-macros than to provide them in C++'s awful half-baked form. - C++-17 (-20) constexpr is one century in advance in front of rust macro in term of power and what you can do with it. It is not even comparable. - C++20 compile time execution is currently pretty clean and has nothing to do with this template mess shown here. Definitively not "half baked".
- tines 6y agoYes. From what I understand, Rust's macros/generics are not Turing complete.
- steveklabnik 6y agoThey are. Procedural macros especially are a function that takes tokens and returns tokens, and have the full language at their disposal.
- tines 6y agoAh interesting. But I imagine that token-based macros don't give you access to the types in the program being compiled do they? i.e. a c++ template can tell when two types are the same but a token level macro wouldn't be able to do that I think.
- steveklabnik 6y agoCorrect.
- roca 6y agoCompile-time function evaluation is quite a different feature from macros. Most of what you would do with macros (e.g. output entirely new definitions of functions, types, etc) can't be done with constexpr at all.
- tines 6y agoAnd vice-versa.
- pjmlp 6y agoC++'s awful half-baked form comes with 40 years of IDEs, libraries, GUI tooling, OS and GPGPU SDKs, so while I do like Rust, I rather use C++ for the time being, as clunky as it might be.
- rowanG077 6y agoYou can do it in Haskell with Template Haskell. You can also lift some computation to the type level with means they will run at compile time during type checking.