As the title says, there's something called Concepts Lite
(Template Constraints) and Concept Design for the STL. Can anybody explain some fundamental differences between these?
The first introduces changes to the C++ language (core) to have facilities for expressing restrictions on templates. Providing sensible checks, good error messages to users and simple tools to template writers.
It will be developed as Technical Specification (TS) and expected by by mid 2014, about same time C++14. Compiler writers seem enthusiastic to ship it soon too.
The second is about library: current standard lib cna't use constraints that are not yet part of the standard for obvious reasons. But once that part is bose, the templates can and will be improved with more native expression of requirements. I.e currently it's only written in the text that template argument must be default-constructible or assignable. So if you use C++03 and std::vector<std::auto_ptr<int> > it's just UB. But with conceptized version such problems will be diagnosed at compile.
Related
Why doesn't C++ allow containers of incomplete types to be instantiated?
It's certainly possible to write containers that don't have this restriction -- boost::container is completely capable of doing this. As far as I can see, it doesn't seem to give any performance or other type of gain, and yet the standard declares it to be undefined behavior.
It does prevent recursive data structures from being built, for example.
Why then does the C++ standard impose this arbitrary restriction? What would have been the downside of allowing incomplete types as template parameters wherever possible?
Matt Austern, the chair of the C++ standardization committee's library working group, explained this decision of the committee in his Dr. Dobb's article by historical reasons:
We discovered, with more testing, that even the [simple] example didn't work with every STL implementation. In the end, it all seemed too murky and too poorly understood; the standardization committee didn't think there was any choice except to say that STL containers aren't supposed to work with incomplete types. For good measure, we applied that prohibition to the rest of the standard library too.
My understanding of this is that the committee did not want to invalidate existing implementations of the library by requiring them to support incomplete types retroactively.
In the same article he concedes that
In a future revision of C++, it might make sense to relax the restriction on instantiating standard library templates with incomplete types.
Given that the article dates back to 2002, and the prohibition remains in place in the current standard, I think that the decision of the boost designers not to wait for the future and build their own containers that allow incomplete types was fully justified.
Edit: See this answer for information on using incomplete types allowed by C++17 standard for some containers in the Standard C++ Library.
Why doesn't C++ allow containers of incomplete types to be instantiated?
It's certainly possible to write containers that don't have this restriction -- boost::container is completely capable of doing this. As far as I can see, it doesn't seem to give any performance or other type of gain, and yet the standard declares it to be undefined behavior.
It does prevent recursive data structures from being built, for example.
Why then does the C++ standard impose this arbitrary restriction? What would have been the downside of allowing incomplete types as template parameters wherever possible?
Matt Austern, the chair of the C++ standardization committee's library working group, explained this decision of the committee in his Dr. Dobb's article by historical reasons:
We discovered, with more testing, that even the [simple] example didn't work with every STL implementation. In the end, it all seemed too murky and too poorly understood; the standardization committee didn't think there was any choice except to say that STL containers aren't supposed to work with incomplete types. For good measure, we applied that prohibition to the rest of the standard library too.
My understanding of this is that the committee did not want to invalidate existing implementations of the library by requiring them to support incomplete types retroactively.
In the same article he concedes that
In a future revision of C++, it might make sense to relax the restriction on instantiating standard library templates with incomplete types.
Given that the article dates back to 2002, and the prohibition remains in place in the current standard, I think that the decision of the boost designers not to wait for the future and build their own containers that allow incomplete types was fully justified.
Edit: See this answer for information on using incomplete types allowed by C++17 standard for some containers in the Standard C++ Library.
'Concepts lite' were already accepted as a TS and (example implementation) merged into GCC main branch, so the follow up question is will any concepts come predefined (like Sortable or Random_access_range)?
Where do I look for such predefined concepts?
Is the list at cppreference.com an acurate and exhaustive list?
Can I use them with the latest GCC trunk build?
Edit 1: Changed C++17 to TS due to concepts not being accepted into C++17.
There are no concepts defined in the Concepts TS (source: I wrote the Concepts TS).
It is neither an oversight nor mistake... The goal was to ship a pure language extension in the TS, allowing developers time to experiment with the new features before committing (an incredible amount of) time defining the concepts needed for the Standard Library.
The Ranges TS will define the concepts needed for the Standard Library.
GCC may ship with some concepts, but I have not heard of any concrete plans to do so. I have a library that defines many of the concepts (but not all) that appear in the Ranges TS here: https://github.com/asutton/origin, but I'm still working on a usable release and appropriate documentation. And it only compiles against GCC from trunk. I'm hoping for adequate documentation by next week.
'Concepts lite' were already accepted for C++17
No, it isn't. It's a separate TS.
will any concepts come predefined?
Not by the Concepts TS, which is limited to the language feature. The current Ranges TS working draft does define a number of concepts.
Why doesn't C++ allow containers of incomplete types to be instantiated?
It's certainly possible to write containers that don't have this restriction -- boost::container is completely capable of doing this. As far as I can see, it doesn't seem to give any performance or other type of gain, and yet the standard declares it to be undefined behavior.
It does prevent recursive data structures from being built, for example.
Why then does the C++ standard impose this arbitrary restriction? What would have been the downside of allowing incomplete types as template parameters wherever possible?
Matt Austern, the chair of the C++ standardization committee's library working group, explained this decision of the committee in his Dr. Dobb's article by historical reasons:
We discovered, with more testing, that even the [simple] example didn't work with every STL implementation. In the end, it all seemed too murky and too poorly understood; the standardization committee didn't think there was any choice except to say that STL containers aren't supposed to work with incomplete types. For good measure, we applied that prohibition to the rest of the standard library too.
My understanding of this is that the committee did not want to invalidate existing implementations of the library by requiring them to support incomplete types retroactively.
In the same article he concedes that
In a future revision of C++, it might make sense to relax the restriction on instantiating standard library templates with incomplete types.
Given that the article dates back to 2002, and the prohibition remains in place in the current standard, I think that the decision of the boost designers not to wait for the future and build their own containers that allow incomplete types was fully justified.
Edit: See this answer for information on using incomplete types allowed by C++17 standard for some containers in the Standard C++ Library.
Why doesn't C++ allow containers of incomplete types to be instantiated?
It's certainly possible to write containers that don't have this restriction -- boost::container is completely capable of doing this. As far as I can see, it doesn't seem to give any performance or other type of gain, and yet the standard declares it to be undefined behavior.
It does prevent recursive data structures from being built, for example.
Why then does the C++ standard impose this arbitrary restriction? What would have been the downside of allowing incomplete types as template parameters wherever possible?
Matt Austern, the chair of the C++ standardization committee's library working group, explained this decision of the committee in his Dr. Dobb's article by historical reasons:
We discovered, with more testing, that even the [simple] example didn't work with every STL implementation. In the end, it all seemed too murky and too poorly understood; the standardization committee didn't think there was any choice except to say that STL containers aren't supposed to work with incomplete types. For good measure, we applied that prohibition to the rest of the standard library too.
My understanding of this is that the committee did not want to invalidate existing implementations of the library by requiring them to support incomplete types retroactively.
In the same article he concedes that
In a future revision of C++, it might make sense to relax the restriction on instantiating standard library templates with incomplete types.
Given that the article dates back to 2002, and the prohibition remains in place in the current standard, I think that the decision of the boost designers not to wait for the future and build their own containers that allow incomplete types was fully justified.
Edit: See this answer for information on using incomplete types allowed by C++17 standard for some containers in the Standard C++ Library.