But these have significant inefficiencies.-a The local "Data" object
needs to be created on the stack and its address is passed to the
external function - then on success, it is copied from the local stack object into the caller's stack where the returned std::optional value is held.-a On failure, the whole returned std::optional is cleared - not
just the "engaged" boolean inside the std::optional implementation.
And is there a good reason for clearing out the payload here when
returning nullopt?-a Or is that just an inefficiency of the gcc standard
C++ library implementation?
My guess would be that it is caused by your payload class having no user-defined constructor. This causes any kind of default initialization
to turn into value-initialization requiring everything to be zeroed out.
If you add an explicit "do nothing" user-defined constructor this
behavior will probably go away. Of course, if the struct is declared in
the C side, this might require jumping through some hoops...
On Mon 4/27/2026 5:47 AM, Andrey Tarasevich wrote:
My guess would be that it is caused by your payload class having no
user-defined constructor. This causes any kind of default
initialization to turn into value-initialization requiring everything
to be zeroed out.
If you add an explicit "do nothing" user-defined constructor this
behavior will probably go away. Of course, if the struct is declared
in the C side, this might require jumping through some hoops...
... although the above does not answer the question of why GCC would try
to apply _any_ kind of initialization to the inactive payload in `std::optional` (even if it helps to suppress it).
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 65 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 08:05:50 |
| Calls: | 862 |
| Files: | 1,311 |
| D/L today: |
1 files (1,366K bytes) |
| Messages: | 264,936 |