-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Is the use of incomplete type correct? #138
Comments
This is explicitly allowed for vector by N4510 which was accepted for C++17 in May. However, that doesn't really apply here, as this is about an object that has a member variable which is a container of an incomplete type. This doesn't happen here. basic_json contains a union which has has pointers to these containers. |
OK, understood. I was wondering why the union stored a pointer instead of an object. |
The compilation failed when using sizeof(..) in custom map type, I have encountered this issue, and I removed that sizeof(..) in my custom map, which was used for a compile time safe check. I think the container value type would be better to use a smart pointer instead of object value. |
@dariomt - I could not get the code to compile when adding |
For that you would need to use containers with support for incomplete types, to be able to create recursive structures. Boost.Container offers that support, but it means not using the STL containers :( In an ideal world, there would be some way to specify if the union should use pointers (for space efficiency) or not use pointers (for memory-allocation efficiency, which would require specifying containers with support for incomplete types). The default usage would be the current "use pointers in union" with STL containers, but I could easily switch to "not use pointers in the union" and specify Boost containers in the basic_json template. |
As of C++17, std::vector supports incomplete types in containers. This support was not added to std::map yet. |
I close this issue now as there is nothing that can be done at this point. |
(Thanks for all the feedback!) |
The definitions of
array_t
andobject_t
in json.hpp look like this:So it is instantiating the
ArrayType
and theObjectType
templates whenbasic_json
is still an incomplete type. By default those templates arestd::vector
andstd::map
.It seems to work, but I wonder, is it correct according to the standard?
Instantiating STL types with incomplete types seems to be prohibited, so I'd say recursive structures like this would not be technically correct (even if the current implementation allowed it).
From an answer in stackoverflow:
In fact, supporting recursive structures is one of the nice features that Boost.Container provides and the STL does not, according to its docs.
What do you think?
The text was updated successfully, but these errors were encountered: