Skip to content
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

Introduce non-explicit empty constructor in C++ #459

Closed
mikir opened this issue Dec 2, 2022 · 1 comment
Closed

Introduce non-explicit empty constructor in C++ #459

mikir opened this issue Dec 2, 2022 · 1 comment
Assignees
Labels
c++ C++ language generator enhancement New feature or request
Milestone

Comments

@mikir
Copy link
Contributor

mikir commented Dec 2, 2022

Currently, Zserio objects in C++ do have the following default constructor:

explicit ZserioObject(const allocator_type& allocator = allocator_type()) noexcept;

This prevents calling of empty constructor implicitly. Example:

ZserioObject zserioObject = {};

This could be potentionally cumbersome for example if ZserioObject is passed as parameter.

The solution could be to split this constructor as follows:

ZserioObject() noexcept;
explicit ZserioObject(const allocator_type& allocator) noexcept;
@mikir mikir added enhancement New feature or request c++ C++ language generator labels Dec 2, 2022
@mikir mikir added this to the 2.10 milestone Dec 2, 2022
@Mi-La Mi-La self-assigned this Dec 2, 2022
@Mi-La
Copy link
Contributor

Mi-La commented Dec 6, 2022

  1. We have split single constructor to empty constructor and constructor with allocator_type
  2. Field constructor was improved to support {} - i.e. default template parameters were added
    • construction with {} cannot work for structures with single field since the constructor is ambiguous (ambiguity with copy constructor...)
      const TestStruct testStruct = TestStruct(10, {}, "sample", {});
  3. Note that when structure has optional fields and {} is used as a field constructor parameter, it DOES NOT mean that optional field is not present (NullOpt) but empty constructor is used to create optional field.
    struct TestStruct
    {
        string strFiedl;
        optional uint32 u32Field;
    };
    
    const TestStruct testStructWithDefaultOptional = TestStruct("sample", {}); // {} does not mean NullOpt!
    const TestStruct testStructWithDefaultOptional = TestStruct("sample", uint32_t()); // same as above
    // vs.
    const TestStruct testStructWithoutOptional = TestStruct("sample", zserio::NullOpt);
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ C++ language generator enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants