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

Strange behavior in move assignment of optional #634

Closed
Chiraffollo opened this issue Nov 15, 2022 · 3 comments
Closed

Strange behavior in move assignment of optional #634

Chiraffollo opened this issue Nov 15, 2022 · 3 comments
Assignees
Labels

Comments

@Chiraffollo
Copy link
Contributor

Dear John,

I've found a strange behavior using the optional class.
If I use the move assignment operator of the optional, the resulting object has always valid flag set and the storage is moved.
Was that intended?
Here is a small example:

#include <iostream>
#include "etl/optional.h"

using MyOptional = etl::optional<std::uint8_t>;

void print(const char* const label, const MyOptional& opt) {
    std::cout << label << ": " << (opt.has_value() ? "true" : "false") << std::endl;
}

MyOptional get_my_optional() {
    MyOptional result = 8;
    result.reset();
    print("before return", result);
    return result;
}

int main(int argc, char** argv) {
    MyOptional opt;
    opt = get_my_optional();
    print("after return", opt);

    return 0;
}

The program results in the output:

before return: false
after return: true

I expected the move assignment to move both valid and storage.

@jwellbelove jwellbelove self-assigned this Nov 16, 2022
@Chiraffollo
Copy link
Contributor Author

Should I fix it and make a pull request?

@jwellbelove
Copy link
Contributor

It was only a one-line change.
I'll push a fix tonight.

@jwellbelove
Copy link
Contributor

Fixed 20.35.2

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

No branches or pull requests

2 participants