Slashdot Mirror


C++ Creator Wants To Solve 35-Year-Old Generic Programming Issues With Concepts (cio.com)

C++ creator Bjarne Stroustrup is arguing that we can improve code by grounding generic programming in concepts -- what's required by a template's arguments. An anonymous reader quotes Paul Krill's report on a new paper by Stroustrup: In concepts, Stroustrup sees the solution to the interface specification problem that has long dogged C++, the language he founded more than 35 years ago. "The way we write generic code today is simply too different from the way we write other code," Stroustrup says... Currently an ISO technical specification, concepts provide well-specified interfaces to templates without runtime overhead. Concepts, Stroustrup writes, are intended to complete C++'s support for generic programming as initially envisioned. "The purpose of concepts is to fundamentally simplify and improve design. This leads to fewer bugs and clearer -- often shorter -- code"...

Concepts, Stroustrup believes, will greatly ease engineers' ability to write efficient, reliable C++ code... The most obvious effect will be a massive improvement in the quality of error messages, but the most important long-term effect will be found in the flexibility and clarity of code, Stroustrup says. "In particular, having well-specified interfaces allows for simple, general and zero-overhead overloading of templates. That simplifies much generic code"

Concepts are already available in GNU C Compiler 6.2, and Stroustrup wants them to be included in C++ 20. "In my opinion, concepts should have been part of C++ 17, but the committee couldn't reach consensus on that."

9 of 339 comments (clear)

  1. As someone with a masters in this -exact field- by Anonymous Coward · · Score: 1, Informative

    Bjarne Stroustrup, Doug Lea, Knuth, etc... still make feel like a moron on a almost daily basis....

    1. Re:As someone with a masters in this -exact field- by ShanghaiBill · · Score: 5, Informative

      Bjarne Stroustrup, Doug Lea, Knuth, etc... still make feel like a moron on a almost daily basis....

      If someone makes you feel like a moron when they explain something, then maybe they are not as smart as you think they are. If you are a true master, you should be able to explain concepts in a way that even a child can understand. Richard Feynman was famous for this. So was Albert Einstein. Of course you can go too far, and simplify too much, so the children only think they understand. Donald Trump is a good example of that.

  2. Re:A new fad? by Anonymous Coward · · Score: 2, Informative

    You're obviously new at reading. t != r

  3. Re:Is it true? by Anonymous+Brave+Guy · · Score: 4, Informative

    I never saw that in the many years I was working primarily with C++ and a regular reader of the related newsgroups. When Bjarne did contribute in any forums I followed, he generally seemed direct and reasonable, and it was usually in the more advanced discussions about tricky areas or the future of the language.

    --
    If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
  4. Re: GAY NIGGER FUCKS ASS WRITTEN IN C AND C++ by Anonymous Coward · · Score: 1, Informative

    This is actually how the GNAA spawns new members. For years, a requirement for joining their group was to first post this spam on slashdot, and it may still be the case. They are a group of dual classed hacker/trolls. As with all the posts, you could be witnessing the birth of a new member, an old member being bored, or a nonmember who just wants to fuck over discussion if possible. In general, it will get modded all the way down, and often deleted. But anyway, this is what these posts have looked like for years.

  5. Re:A new fad? by serviscope_minor · · Score: 5, Informative

    No, not a new fad.

    Concepts have been coming in C++ since the 90s.

    There are two parts of concepts. On the ideas side, Stapanov came up with the idea with the STL. A concept of X is anything X-like. So loosely speaking if you have an array concept, then anything giving [] and having indexable pointers/iterators matches. So, builtin arrays match, as does std::array and vectors from Eigen would match too.

    This makes sense: they're semantically the same and if you wrote an algorithm (eg. sort) using any of them, it would look identical. In this way, it's like a specification. If your class matches the specified "array" concept, then any algorithm written against that spec (the array concept) will work correctly on your class.

    It's a great idea.

    The other bit is language support. So, much C++ code is written using the idea of concepts, but the language does not assist in any way. Templating is completely generic, you say essentially "accept any class", but if you've written against the array concept, and the class doesn't match, you'll get a compile error right in the guts of the algorithm where you try to use [] on an array.

    It would be nice, instead of saying "this function accepts any class" to say "this function accepts any class matching the Array concept", or in short "this function accepts any Array". The compiler knows the types and can tell in advance if the class matches. That way if you try to sort a set with that function, for example, it would simply tell you that the set is not an Array.

    You can kind of finesse and finagle something better by utterly abusing a different language feature. The language has a mechanism called SFINAE (substitution failure is not an error), which is designed to make function overloading in the presence of templates sane. When the compiler does overload resolution, it substitutes the current type into any templated functions which match the name in question. If that substitution yields a compiler error, the compiler ignores it and removes that option from the list of overloads. This prevents unrelated templated overloads from breaking builds in irrelevant places. It's supposed to be a hidden detail which makes templating "just work". Except you can abuse it to enable/disable functions based on compile time tests for features of a class. IOW you can use it to implement concepts. Accordding to Stroustrup this means "you know too much :(".

    He's right: just because you can do it like that doesn't mean it makes life easy.

    Putting concepts into the language gives explicit support, with clean, clear, consistent syntax, not using brittle SFINAE hacks. People have been working out how since the 90s, and there have been several major proposals. It's not actually that trivial. You have to avoid breaking any old code, no matter how perverse. It has to work and work cleanly with old, non concept aware code (otherwise it would get slow/no adoption) and it needs to make life simpler for language users and not be an expert only feature, etc etc.

    It looks like the concepts TS is finally hitting enough of the targets to actually work.

    So basically, no, concepts is not a fad. It's been a major part of the C++ world for 2 decades.

    --
    SJW n. One who posts facts.
  6. Re: A new fad? by TheRaven64 · · Score: 4, Informative

    Yes, except with compile-time specialisation instead of run-time specialisation. One of the big problems that I have with C++ is that it has entirely separate mechanisms and syntax for implementing the same thing with compile-time and run-time specialisation and they don't always compose well. Languages such as Java sidestep this by providing only run-time specialisation and expecting the JIT compiler to generate the equivalent of compile-time specialisation.

    With an abstract class in C++, you'd require that every method be called via a vtable, which makes inlining hard (though modern compiler can do devirtualisation to some extent). This often doesn't matter, but when it's something like an array access, which is 1-2 instructions, the cost of the method call adds up. In contrast, if you use a template then the compiler knows exactly which method implementation is called and will inline any trivial methods (at the cost of now having one version of each templated function for every data type, which can blow away your instruction cache if you're not careful). The down side of the template approach is that you have no (simple) way of saying 'this template argument must be a thing on which these operations are defined' and the error message when you get it wrong is often dozens of layers of template instantiation later and totally incomprehensible without a tool such as Templight.

    --
    I am TheRaven on Soylent News
  7. Re:More features. by TheRaven64 · · Score: 4, Informative

    For embedded systems, you really don't want exceptions. The runtime for RTTI and exceptions is bigger than the flash on most systems (I wrote the one that ships on FreeBSD, the PS4, and a few other places - it's not a huge amount of code in the context of a desktop OS, but it's 100KB of object code for the C++ bits, plus the generic stack unwinder - you don't want to burn 150-200KB of space on an embedded system for this) and stack unwinding performance is very hard to reason about in anything realtime. The reason that the Embedded C++ subset excluded templates was that they make it very hard to reason about code size. A small amount of source code can easily become 10-100KB of object code if you instantiate templates with too many different types. Writing foo() is now not a simple case of set up the call stack and jump, it's either that simple if someone else has instantiated the same template function, or it's creating an entirely new copy of foo and all other templates that it refers to using the template parameters. This makes it very difficult to work out what changes were responsible for pushing the size above the available space. Actually, it's even worse, because the specialised function might now be simple enough to inline everywhere and give an overall space saving, but reasoning about exactly where that balance is becomes very hard. It's not that C++ generates bigger code than C, it's that object code size in C++ has far less of a direct correspondence with source code size than C.

    --
    I am TheRaven on Soylent News
  8. Re: A new fad? by vyvepe · · Score: 3, Informative

    The man difference between a concept and an interface is in the time when the dispatching to some specific called code is resolved.

    Concepts resolve the call during compile time. This can lead to binary code bloat since the calling code needs to be "cloned" for each called code.

    Interfaces resolve the call during run time. It can reuse the caller code but imposes some call overhead (the run time dispatch).

    And sometimes you just need the resolving in the run time. If it would not be available then one would be forced to simulate it ... e.g. using dispatch (switch) on e.g. an alternative type (discriminated union). Well, or you could implement vtable manually if the language gives you enough power (pointer arithmetic) to do it.