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

Copy constructor for C++ Object API #5783

Closed
cdfh opened this issue Feb 28, 2020 · 5 comments
Closed

Copy constructor for C++ Object API #5783

cdfh opened this issue Feb 28, 2020 · 5 comments
Labels

Comments

@cdfh
Copy link

cdfh commented Feb 28, 2020

Flatbuffer objects in the C++ Object API cannot be copied if they include other tables as elements due to the use of std::unique_ptr to reference the other tables.

For example, the schema

table Bar {
...
}

table Foo {
  bars: [Bar];
}

compiles to

struct FooT : public flatbuffers::NativeTable {
  ...
  std::vector<flatbuffers::unique_ptr<BarT>> bars;  // resolves to std::unique_ptr
  ...
};

noting that std::unique_ptr disallows copying. Thus, I cannot copy instances of FooT without considerable code verbosity (e.g., packing and unpacking the FooT instance).

My understanding is that the Object API is designed to increase convenience in instances where non-copy constraints are relaxed, and so it seems a bug not to bless them with a copy constructor.

@aardappel
Copy link
Collaborator

I agree that generating an explicit copy contructor would make sense.. care to create a PR for that?

@github-actions
Copy link

This issue is stale because it has been open 6 months with no activity. Please comment or this will be closed in 14 days.

@github-actions github-actions bot added the stale label Aug 31, 2020
@cdfh
Copy link
Author

cdfh commented Aug 31, 2020

Commenting to prevent the issue from automatically closing. Looks like a fix is imminent though still pending with #5988.

@github-actions github-actions bot removed the stale label Sep 1, 2020
@jfroy
Copy link
Contributor

jfroy commented Sep 14, 2020

Thanks. Sorry for the lack of updates, I've been busy. Will get to it...

@github-actions
Copy link

This issue is stale because it has been open 6 months with no activity. Please comment or this will be closed in 14 days.

@github-actions github-actions bot added the stale label Mar 15, 2021
dbaileychess pushed a commit that referenced this issue Jan 29, 2022
Augment the C++ generator to emit a C++ copy constructor and a by-value
copy assignment operator. This is enabled by default when the C++
standard is C++11 or later. These additional functions are only emitted
for objects that need it, typically tables containing other tables.

These new functions are declared in the object table type and are
defined as inline functions after table declarations.

When these new functions are declared, a user-defined
explicitly-defaulted default constructor and move constructor are also
emitted.

The copy assignment operator uses the copy-and-swap idiom to provide
strong exception safety, at the expense of keeping 2 full table copies
in memory temporarily.

fixes #5783
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants