recursive struct using std::deque and other containers [duplicate] - c++

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.

Related

C++ How to declare deque of classes within the same class? [duplicate]

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.

Declaring vector type as struct in same struct [duplicate]

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 C++ containers don't allow incomplete types?

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.

What's the difference between C++ Concepts and C++ Template Constraints?

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.

Does C++11 add the C99 restrict specifier? If not, why not?

restrict is a C99 feature which is getting a lot of attention lately by allowing the compiler to perform "previously-fortran-only" optimizations to pointers. It's also the same keyword announced by Microsoft recently to be the underpinnings of the C++AMP specification.
Is that keyword actually in the FCD? If not, is there a specific reason it was omitted?
The only mention of restrict in the C++11 FDIS is on §17.2 [library.c]:
The descriptions of many library functions rely on the C standard library for the signatures and semantics
of those functions. In all such cases, any use of the restrict qualifier shall be omitted.
So restrict is not in C++11.
One argument is that C needs restrict more than C++, because many operations are done with pointers to primitive types and therefore C code has more aliasing problems than C++.
The aliasing rules say that pointers to different types cannot alias, so if the parameters to a function are of different class types they just cannot overlap.
In C++ we also have the valarray family of classes that are supposed to handle arrays of primitive types that are not allowed to alias. Not that it is used much...
Adding yet another way to resolve some aliasing problems, obviously didn't excite the committee enough.
http://herbsutter.com/2012/05/03/reader-qa-what-about-vc-and-c99/
Not only the VC++ team, but also the ISO C++ standards committee, considered adding restrict to VC++ and ISO C++, respectively. Although it was specifically suggested for ISO C++11, it was rejected, in part because it’s not always obvious how it extends to C++ code because C++ is a larger language with more options and we would want to make sure the feature works correctly across the entire language.
Don't think it's in C++1x (unfortunately time has long run out for 0x...!) but at least msvc and g++ support it through __restrict and __restrict__ extensions. (I don't use gcc much, I think that's the correct extension).
To work properly with C++ I feel that we would also need restricted references, not just pointers, maybe along the lines of my question C++ aliasing rules. Not sure if some of these considerations might be holding things up...
I will take a crack at "why not?"
restrict is basically just an assertion that the compiler cannot verify. (Or more precisely, when the compiler can verify it, the assertion itself is not helpful.) This is just not the sort of thing that the C++ committee is going to like. C++ has always tended to assume "sufficiently smart compilers"; heck, look at the hideous performance of the most trivial C++ libraries before the compilers caught up.
I also suspect the committee felt that defining restrict semantics precisely in the presence of all the other C++ features (references, rvalue references, blah blah blah) would be non-trivial.
So, non-trivial to specify + "a sufficiently smart compiler doesn't need it" = NAK.