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

[BUG] is type for std::optional can return unexpected results #1328

Open
bluetarpmedia opened this issue Nov 1, 2024 · 0 comments
Open

[BUG] is type for std::optional can return unexpected results #1328

bluetarpmedia opened this issue Nov 1, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@bluetarpmedia
Copy link
Contributor

Describe the bug
An expression testing if a std::optional is a type can return unexpected results, depending on whether the optional's type is convertible to the target type. (I'm assuming this behaviour is unintended.)

To Reproduce
Run cppfront on the following and then compile with a C++ compiler:

main: () -> int = {
    o: std::optional<int> = 5;
    if o is int {
        std::cout << "int!\n";
    }
    if o is bool {
        std::cout << "bool!\n";
    }
    if o is float {
        std::cout << "float!\n";
    }
    if o is std::string {
        std::cout << "std::string!\n";
    }
    return 0;
}

The output is:

int!
bool!
float!

Repro on Godbolt.

I think this behaviour is due to this cast:

if constexpr (requires { static_cast<const T&>(*x);}) {

CC @filipsajdak

@bluetarpmedia bluetarpmedia added the bug Something isn't working label Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant