7 ms·
As someone that really likes C++ templates (no other language seems to come close in power for such a compile-time typed system), I'm particularly looking forwa
by AlexSW 6y ago
As someone that really likes C++ templates (no other language seems to come close in power for such a compile-time typed system), I'm particularly looking forward to concepts, and I believe (correct me if I'm wrong) that they will make compiler errors for templated code much more straightforward, which will be a nice quality of life improvement even if not using concepts directly.
- greggman3 6y agoThat strikes me as Stockholm Syndrome. No one in their right mind would consider C++ templates a good design for a meta-language. In fact the majority of older template patterns aren't based on good language design but hacks around conditional compiling via arrays of size -1 and crap like that. A good meta programming language would be one that's as easy to program as a general language. lisp, nim, etc...
- einpoklum 6y agoIt's true the templates weren't designed as a meta language. C++ has a lot of baggage. But, at least, you no longer have to iterate over arrays with size-1, (size-1)-1 etc these days. We have variadic template expansion, we have the (ugly, still not decent design, but useful) std::integer_sequence , and better still - we have pretty strong constexpr capabilities.
- Expert-007 6y agoSuch examples speak more to C++ templates as a mechanism for capturing usage patterns in a convenient form, thus effectively creating a special-purpose, domain-specific language, than they speak to such templates as a meta-language. "Meta" means "information about information", so a meta-language in this case would describe the mechanisms used to capture such patterns, not the patterns themselves. I do meta every day, meta-meta often, and triple-meta occasionally. The rules language for my Expert System is a true meta-language, because I designed it to (among other things) specify the manipulation of computer languages. So its domain knowledge is about the nature of computer languages and how to automate their assessment / analysis, transformation / re-engineering, translation, and generation. To give a real-life example, we used our Expert System to automate the translation of complex compiler front- and back-end code from a proprietary system language to C++ for a major computer manufacturer. The translation rules don't really care what the code is doing; they care how the code is doing it. The compiler team created new templates and classes to provide the functionality their system language had that C++ doesn't, and we adjusted our translation rules to ensure that the translated code invokes those templates and classes in a clear, effective, efficient, and readable way.
- HelloNurse 6y agoExactly, old hacks and techniques. Thirty years ago, C++ template were limited and clever people struggled with their limitations, but now there are vastly simpler ways to do much more.
- _se 6y agoI think this is.. quite wrong. I don't think C++ is an amazing language (it's not in my top 3 personally, but I do work with it daily), but templates are one of the best things about the language IMO. I have a lot of experience with many different types of metaprogramming (cl macros, Rust macros, template Haskell (and Typeable/Generic), ... even CLR code generation) and C++ templates are better than everything I've used other than Common Lisp once you really understand them. A really common C++ theme I see is people that don't understand things railing against them (I used to be in this camp myself, so I can identify with the sentiment). Templates allow you to choose at will between nominal/structural subtyping (in a way), perform type-level logic, generate code that can be thread-safe or lock-free at the caller's whim... They are very powerful, much more than most devs realize, and if you understand C++, writing them is not difficult. The "really understand them" part is, of course, the hard part. You need to understand the core language and the template language to be effective with them. I can see why even some experienced C++ developers shy away from templates.
- Dylan16807 6y agoEven if they're a good general design, half the uses of SFINAE are absolutely insane and from what I understand it's significantly harder than it should be to make a fully generic template across all kinds of edge case and corner case types. The attitude with templates seems to be that if something is at all possible with hacks then there doesn't need to be any language support added, even if it would be vastly simpler to read and write and probably to compile. I don't think that works out well.
- northwindfoo 6y agoI wouldn't call myself a c++ expert but I have used the language for a long time. In my opinion, templates have a lot of awesome uses outside of SFINAE and if you find yourself reaching for SFINAE, that's probably code smell and you can probably simplify the design elsewhere to avoid it. Generally I find the best use of templates is the most basic: you want to write some code that operates on duck-types. As long as one is able to control themselves and pick the right use cases for maintainability, templates are simple enough. In my experience, this is very doable. Concepts are also a really cool way to rein in which types are accepted as a template parameter and have a lot of potential to make templates easier to understand and teach. To me, this is the most exciting c++20 feature.
- otabdeveloper4 6y ago> ... easy to program as a general language. lisp, nim, etc... C++ templates are just a compile-time lazily-evaluated Lisp. As always, the criticism of C++ comes down to "programming is hard, let's go shopping". If you don't want to learn and you just want to do the bare minimum to keep your 8 hour coding job, there's already plenty of languages that nail this niche. C++ obviously is not for you, just move on and don't make a redundant comment.
- greggman3 6y agoWhat an asinine comment. I guess if you're real programmer you'll program everything in brainfuck. Isn't not hard to fathom that some language designs are easier to express and easier to understand than others and still get the same things done. C++'s meta programming via templates is clearly a hack and was never designed to do what it's being abused to do.
- zarkov99 6y agoOther than lisp, which production ready languages do you think do it better?
- rwmj 6y agoI really liked camlp4 (an early fork of which is available as camlp5, but no longer a fundamental part of the OCaml ecosystem). I was able to use camlp4 to implement Erlang-style bitstrings, and they were as efficient as if you had written the most efficient code by hand. https://people.redhat.com/~rjones/bitstring/html/Bitstring.html https://people.redhat.com/~rjones/bitstring/html/Bitstring.h...
- SloopJon 6y agoI consciously avoided C++ since the late 90s, lest its litany of accidental complexities crowd other more important things out of my brain. I was drawn into it recently by a work assignment, and have been pleasantly surprised by modern C++. I find it much more pleasant to write, e.g., ODBC client code in C++ than in C, and templates have a lot to do with that. I'm not an expert with Lisp macros or other reportedly superior meta-languages, but I will say that when I write generic code in C# or Java, I really miss the power of C++ templates. Template syntax is pretty awful, and accomplishing what I want can involve some really strange idioms, like SFINAE. The ergonomics are slowly improving with type deduction, constexpr, and now concepts.
- gumby 6y agoAs Stroustrup said to me years ago, “yes, I wanted a macro system but that word had already been colonized by the preprocessor, and we needed to avoid new keywords as much as possible if people were going to try it out. Nowadays C++ and a powerful multi-paradigm programming language quite straightforward to use if you forget the word “C” in its name. If would be a good instructional language if people didn’t fall into the lazy “a horse is a king of animal” OO trope.
- dgellow 6y ago> I believe (correct me if I'm wrong) that they will make compiler errors for templated code much more straightforward Oh yes, that would really be awesome if that's the case. Wikipedia has a section regarding "Compiler diagnostics" when Concepts are used, the precision of the error message demonstrated is SO MUCH better than what you get with templates! https://en.wikipedia.org/wiki/Concepts_(C%2B%2B)#Compiler_diagnostics https://en.wikipedia.org/wiki/Concepts_(C%2B%2B)#Compiler_di... Code example: std::list<int> l = {2, 1, 3}; std::sort(l.begin(), l.end()); Example with templates: In instantiation of 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::_List_iterator<int>; _Compare = __gnu_cxx::__ops::_Iter_less_iter]': error: no match for 'operator-' (operand types are 'std::_List_iterator<int>' and 'std::_List_iterator<int>') std::__lg(__last - __first) * 2, ... ... // continues for 50+ lines Example with concepts: error: cannot call function 'void std::sort(_RAIter, _RAIter) [with _RAIter = std::_List_iterator<int>]' note: concept 'RandomAccessIterator()' was not satisfied
- HelloNurse 6y agoStraightforward error messages are only an aspect of straightforward template definitions: the problem of restricting templates to suitable type parameters has the first general, explicit and well organized solution.
- mhh__ 6y ago> no other language seems to come close in power for such a compile-time typed system D is far more powerful, especially when you consider that it's had working constraints for (?) something like a decade now. Simple example, try to express this in C++ without a windows full of syntax-soup (https://d.godbolt.org/z/Toc8ob https://d.godbolt.org/z/Toc8ob), the template constraint is one line, and I can pass anything into a template using alias be that templates, a string etc. Passing strings into a templates is a very common trick for generating code at compile time in the D standard library, although it wouldn't work in C++ due to how slow C++ can be to compile some times.
- pjmlp 6y agoSadly, D the language still needs to get their act together in what the language actually wants to be, and the eco-system suffers from this lack of direction.
- nikki93 6y ago> Passing strings into a templates ... wouldn't work in C++ ... https://www.youtube.com/watch?v=QM3W36COnE4 https://www.youtube.com/watch?v=QM3W36COnE4 Here's an example of parsing regular expressions in C++ at compile time. Code here: https://github.com/hanickadot/compile-time-regular-expressions https://github.com/hanickadot/compile-time-regular-expressio...